﻿using CommonServiceLocator;
using GalaSoft.MvvmLight.Messaging;
using Jn698._45GWTool.Communication;
using Jn698._45GWTool.Models;
using Jn698._45GWTool.Models.DataGrid;
using Jn698._45GWTool.ServiceReference1;
using Jn698._45GWTool.Services;
using Jn698._45GWTool.ViewModels;
using Jn698._45GWTool.ViewModels.Controls;
using Jn698._45GWTool.ViewModels.Controls.DataControls;
using Jn698._45Tool.Service;
using nblTool.utils;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Documents;
using System.Xml.Linq;
using TouchSocket.Core;
using WebFirst.Entities;
using WindowsFormsApp1.Utils;
using static Communication.MessageGenerationModule;

namespace Communication
{
    public class AnalysisMessage
    {
        public static int analysisPoint = 0;
        private static EnumInfoService enumInfoService = new EnumInfoService();
        private static DataAccessResultService dataAccessResultService = new DataAccessResultService();
        private static BatchParaConfigService BatchParaConfigService = new BatchParaConfigService();
        private static GWCmdMakeUpPlanService GWCmdMakeUpPlanService = new GWCmdMakeUpPlanService();
        private static OIService oIService = new OIService();
        public static string AnalysisResultData;
        private static SendModule sendModule = new SendModule();
        public static byte lastblock = 1;
        public static short block = 0;
        /// <summary>
        /// 获取698有效帧
        /// </summary>
        /// <param name="frame"></param>
        /// <returns></returns>
        public static byte[] GetValidFrameByte(byte[] frame)
        {
            
            if (frame == null|| ByteUtils.byteTrimEnd(frame).Length<6)
            {
                return null;
            }
            short frameLen = 0;
            int len = 0;
            if (frame[0] == 0xfe)
            {
                 frameLen = BitConverter.ToInt16(new byte[] { frame[5], frame[6] }, 0);
                len=frameLen + 6;
            }
            else
            {
                 frameLen = BitConverter.ToInt16(new byte[] { frame[1], frame[2] }, 0);
                len=frameLen + 2;
            }
            
            //int len = frameLen+2;
            if (frame.Length< len)
            {
                return null;
            }
            byte[] recivedata = new byte[len];
            int rcvdatalen = 0;
            int step = 0;
            try
            {
                for (int i = 0; i < len; i++)
                {
                    if (frame[i] == 0x68 && step == 0)
                    {
                        recivedata[rcvdatalen++] = frame[i];
                        step = 1;
                        continue;

                    }
                    else if (frame[i] == 0x16)
                    {
                        if (step == 1)
                        {
                            recivedata[rcvdatalen++] = frame[i];
                            
                            if (frameLen == rcvdatalen - 2)
                            {
                                step = 2;
                                break;
                            }

                            continue;
                        }
                    }
                    else if (step == 1)
                    {
                        recivedata[rcvdatalen++] = frame[i];
                    }
                }
                if (step == 2)
                {
                    return recivedata;
                }
            }
            catch (Exception)
            {

                return null;
            }

            return null;
        }
        private static string GetOADInfo(byte[] frame)
        {
            StringBuilder sb = new StringBuilder();
            //记录型对象属性描述符    	OAD
            sb.Append("对象属性描述符：");
            string OI = "";
            string OAD = "";
            int attributeID = 0;
            int index = 0;
            string Oiname = "";
            for (int i = 0; i < 4; i++)
            {
                if (i < 2)
                {
                    OI += frame[analysisPoint].ToString("X2");
                }
                else if (i == 2)
                {
                    attributeID = frame[analysisPoint];
                }
                else
                {
                    index = frame[analysisPoint];
                }
                OAD += frame[analysisPoint].ToString("X2");
                sb.Append(frame[analysisPoint].ToString("X2"));
                analysisPoint++;
            }
            var OiInfo = oIService.SelectNameByOI(OI);
            if (OiInfo != null)
            {
                var l = SqlSugarHelper.Db.Queryable<ClassAttribute>().Where(c => c.MAttribute == attributeID).Where(c => c.MClass == OiInfo.MIC).Select(c => c.MName).ToList();
                if (l.Count > 0)
                {
                    sb.Append($"——{OiInfo.MName}—{l[0]}\r\n");
                }
                else
                {
                    sb.Append("\r\n");
                }
                Oiname = OiInfo.MName;
            }
            else
            {
                sb.Append("\r\n");
            }
            return sb.ToString();
        }
        public static string Analysis()
        {
            StringBuilder sb = new StringBuilder();

            return sb.ToString();
        }
        public static string AnalysisAPDU(byte[] frame, int lenPoint)
        {
            //analysisPoint = 18;
            analysisPoint = lenPoint;
            if (analysisPoint >= frame.Length)
            {
                return null;
            }
            StringBuilder sb = new StringBuilder();
            sb.Append("--------解析--------\r\n");
            switch (frame[analysisPoint++])
            {
                case 0x82:
                    sb.Append("预连接响应\r\n");
                    sb.Append("服务序号-优先级-ACD:" + frame[analysisPoint++].ToString("X2") + "\r\n");
                    sb.Append(Analysis_Connect(frame));
                    break;
                case 0x83:
                    sb.Append("断开应用连接响应\r\n");
                    sb.Append("服务序号-优先级-ACD:" + frame[analysisPoint++].ToString("X2") + "\r\n");
                    sb.Append(Analysis_DisConnect(frame));
                    break;
                //读取服务
                case 0x85:
                    switch (frame[analysisPoint++])
                    {
                        case 1:
                            sb.Append("读取一个对象属性的响应\r\n");
                            sb.Append("服务序号-优先级-ACD:" + frame[analysisPoint++].ToString("X2") + "\r\n");
                            sb.Append(Analysis_Response(frame, 0x85));
                            break;
                        case 2:
                            sb.Append("读取若干个对象属性的响应\r\n");
                            sb.Append("服务序号-优先级-ACD:" + frame[analysisPoint++].ToString("X2") + "\r\n");
                            int num = frame[analysisPoint++];
                            sb.Append($"共有{num}个对象属性及其数据\r\n");
                            for (int i = 0; i < num; i++)
                            {
                                sb.Append($"第{i + 1}个对象属性及其数据\r\n");
                                sb.Append(Analysis_Response(frame, 0x85));
                            }
                            break;
                         case 3:
                            sb.Append("读取一个记录型对象属性的响应\r\n");
                            sb.Append("服务序号-优先级-ACD:" + frame[analysisPoint++].ToString("X2") + "\r\n");
                            sb.Append(Analysis_Record(frame));
                            break;
                        case 4:
                            sb.Append("读取若干个记录型对象属性的响应\r\n");
                            sb.Append("服务序号-优先级-ACD:" + frame[analysisPoint++].ToString("X2") + "\r\n");
                            num = frame[analysisPoint++];
                            sb.Append($"共有{num}个记录型对象属性及其数据\r\n");
                            for (int i = 0; i < num; i++)
                            {
                                sb.Append($"第{i + 1}个记录型对象属性及其数据\r\n");
                                sb.Append(Analysis_Record(frame));
                            }
                            break;
                        case 5:
                            sb.Append(Analysis_Next(frame));
                            break;
                        default:
                            break;
                    }
                    break;
                case 0x86:
                    switch (frame[analysisPoint++])
                    {
                        case 1:
                            sb.Append("设置一个对象属性的确认信息响应\r\n");
                            sb.Append("服务序号-优先级-ACD:" + frame[analysisPoint++].ToString("X2") + "\r\n");
                            sb.Append(Analysis_Response(frame, 0x86));
                            break;
                        case 2:
                            sb.Append("设置若干个对象属性的确认信息响应\r\n");
                            sb.Append("服务序号-优先级-ACD:" + frame[analysisPoint++].ToString("X2") + "\r\n");
                            int num = frame[analysisPoint++];
                            sb.Append($"共有{num}个对象属性及其数据\r\n");
                            for (int i = 0; i < num; i++)
                            {
                                sb.Append($"第{i + 1}个对象属性及其数据\r\n");
                                sb.Append(Analysis_Response(frame, 0x86));
                            }

                            break;
                        default:
                            break;
                    }
                    break;
                case 0x87:
                    switch (frame[analysisPoint++])
                    {
                        case 1:
                            sb.Append("操作一个对象属性的确认信息响应\r\n");
                            sb.Append("服务序号-优先级-ACD:" + frame[analysisPoint++].ToString("X2") + "\r\n");
                            sb.Append(Analysis_Response(frame, 0x87));
                            break;
                        case 2:
                            sb.Append("操作若干个对象属性的确认信息响应\r\n");
                            sb.Append("服务序号-优先级-ACD:" + frame[analysisPoint++].ToString("X2") + "\r\n");
                            int num = frame[analysisPoint++];
                            sb.Append($"共有{num}个对象属性及其数据\r\n");
                            for (int i = 0; i < num; i++)
                            {
                                sb.Append($"第{i + 1}个对象属性及其数据\r\n");
                                sb.Append(Analysis_Response(frame, 0x86));
                            }

                            break;
                        default:
                            break;
                    }
                    break;
                //68 59 00 83 05 06 00 00 26 08 20 00 EB A6 88 02 16 01 31 18 02 00 06 00 20 22 02 00 00 20 1E 02 00 00 20 20 02 00 00 20
                //24 02 00 00 33 00 02 00 00 33 02 02 06 01 01 06 00 00 00 B8 1C 07 E8 04 1E 09 08 28 1C FF FF FF FF FF FF FF 00 01 00 01 01 51 40 02 02 00 00 00 95 79 16
                case 0x88:
                    switch (frame[analysisPoint++])
                    {
                        case 1:
                            sb.Append("上报若干个对象属性\r\n");
                            sb.Append("服务序号-优先级-ACD:" + frame[analysisPoint++].ToString("X2") + "\r\n");
                            int num = frame[analysisPoint++];
                            sb.Append($"共有{num}个对象属性及其数据\r\n");
                            for (int i = 0; i < num; i++)
                            {
                                sb.Append($"第{i + 1}个对象属性及其数据\r\n");
                                sb.Append(Analysis_Response(frame, 0x85));
                            }
                            break;
                        case 2:
                            sb.Append("上报若干个记录型对象属性\r\n");
                            sb.Append("服务序号-优先级-ACD:" + frame[analysisPoint++].ToString("X2") + "\r\n");
                            num = frame[analysisPoint++];
                            sb.Append($"共有{num}个记录型对象属性及其数据\r\n");
                            for (int i = 0; i < num; i++)
                            {
                                sb.Append($"第{i + 1}个记录型对象属性及其数据\r\n");
                                sb.Append(Analysis_Record(frame));
                            }

                            break;
                        default:
                            break;
                    }
                    break;
                case 0x89:
                    switch (frame[analysisPoint++])
                    {
                        case 7:
                            sb.Append("透明转发命令\r\n");
                            sb.Append("服务序号-优先级-ACD:" + frame[analysisPoint++].ToString("X2") + "\r\n");
                            sb.Append(Analysis_Response(frame, 0x89));
                            break;
                        
                        default:
                            break;
                    }
                    break;
                case 0x90:
                    string mac="";
                    string cTaskData;
                    string outData;
                    StateGridClient sc = ServiceLocator.Current.GetInstance<MainViewModel>().sc;
                    int r = -1;
                    switch (frame[analysisPoint++])
                    {
                        case 0:
                            sb.Append("明文应用数据单元\r\n");
                            int apduLen = frame[analysisPoint++];
                            cTaskData = "";
                            for (int i = 0; i < apduLen; i++)
                            {
                                cTaskData += frame[analysisPoint + i].ToString("X2");
                            }
                            sb.Append(AnalysisAPDU(frame, analysisPoint));

                            if (frame[analysisPoint++] == 0x00)
                            {
                                sb.Append("无数据验证信息\r\n");
                            }
                            else
                            {
                                if (frame[analysisPoint++] == 0x00)
                                {
                                    sb.Append("数据MAC:");
                                    int macLen = frame[analysisPoint++];
                                    mac = "";
                                    for (int i = 0; i < macLen; i++)
                                    {
                                        mac += frame[analysisPoint++].ToString("X2");
                                        // sb.Append(frame[analysisPoint++].ToString("X2"));

                                    }
                                    sb.Append(mac + "\r\n");
                                    if (ServiceLocator.Current.GetInstance<MainViewModel>().SelectedDevice.DeviceType.Equals("电表"))
                                    {
                                        Messenger.Default.Send<string>($"({DateTime.Now.ToString()})：请求加密机服务中...", "Info");
                                        r = sc.Obj_Meter_Formal_VerifyMeterData(1, 1, "", SendModule.SessionInitKey, cTaskData, mac, out outData);
                                        Messenger.Default.Send<string>($"({DateTime.Now.ToString()})：请求加密机服务完成", "Info");
                                    }
                                    else
                                    {
                                        Messenger.Default.Send<string>($"({DateTime.Now.ToString()})：请求加密机服务中...", "Info");
                                        /*if (ServiceLocator.Current.GetInstance<MainViewModel>().SecuritySelectedIndex==1)
                                        {
                                            r = sc.Obj_Terminal_Formal_VerifyReadData(1, 1, SendModule.ESAMDiv, MessageGenerationModule.RN, cTaskData, mac, out outData);
                                        }*/
                                        if (SendModule.SecuritySelectedIndex == 1)
                                        {
                                            r = sc.Obj_Terminal_Formal_VerifyReadData(1, 1, SendModule.ESAMDiv, MessageGenerationModule.RN, cTaskData, mac, out outData);
                                        }
                                        else
                                        {
                                            r = sc.Obj_Terminal_Formal_VerifyTerminalData(1, 1, SendModule.ESAMDiv, SendModule.SessionInitKey, cTaskData, mac, out outData);
                                        }
                                        
                                        Messenger.Default.Send<string>($"({DateTime.Now.ToString()})：请求加密机服务完成", "Info");
                                    }
                                    if (r == 0)
                                    {

                                        sb.Append("明文MAC验证成功\r\n");
                                    }
                                    else
                                    {
                                        sb.Append($"明文MAC验证失败，错误码:{r}，信息{outData}\r\n");
                                    }
                                    sb.Append("\r\n");
                                }
                            }
                            break;
                        case 1:
                            sb.Append("密文应用数据单元\r\n");
                            apduLen = frame[analysisPoint++];
                            cTaskData = "";
                            for (int i = 0; i < apduLen; i++)
                            {
                                cTaskData += frame[analysisPoint + i].ToString("X2");
                            }
                            sb.Append($"密文数据_{apduLen}_{cTaskData}\r\n");
                            if (frame[analysisPoint + apduLen] == 1)
                            {
                                analysisPoint++;
                                if (frame[analysisPoint + apduLen] == 0)
                                {
                                    analysisPoint++;
                                    int macLen = frame[apduLen + analysisPoint++];
                                    mac = "";
                                    for (int i = 0; i < macLen; i++)
                                    {
                                        mac += frame[apduLen + analysisPoint++].ToString("X2");

                                    }
                                    sb.Append($"数据MAC:MAC_{macLen}_{mac}\r\n");
                                }
                            }

                            if (ServiceLocator.Current.GetInstance<MainViewModel>().SelectedDevice.DeviceType.Equals("电表"))
                            {
                                Messenger.Default.Send<string>($"({DateTime.Now.ToString()})：请求加密机服务中...", "Info");
                                if (sc.Obj_Meter_Formal_VerifyMeterData(1, 2, "", SendModule.SessionInitKey, cTaskData, "", out outData) == 0)
                                {
                                    sb.Append("解密后的APDU明文\r\n");
                                    sb.Append(AnalysisAPDU(StringUtils.hexStringToBytes(outData), 0));
                                }
                                Messenger.Default.Send<string>($"({DateTime.Now.ToString()})：请求加密机服务完成", "Info");
                            }
                            else
                            {
                                Messenger.Default.Send<string>($"({DateTime.Now.ToString()})：请求加密机服务中...", "Info");
                                r = sc.Obj_Terminal_Formal_VerifyTerminalData(SendModule.iKeyState, 2, SendModule.ESAMDiv, SendModule.SessionInitKey, cTaskData, mac, out outData);
                                if (r == 0)
                                {
                                    sb.Append("解密后的APDU明文\r\n");
                                    sb.Append(AnalysisAPDU(StringUtils.hexStringToBytes(outData), 0));
                                }
                                else
                                {
                                    MessageBox.Show($"解密数据失败，错误码:{r}");
                                }
                                Messenger.Default.Send<string>($"({DateTime.Now.ToString()})：请求加密机服务完成", "Info");
                            }
                            
                            break;
                        case 2:
                            sb.Append($"异常错误:{frame[analysisPoint++]}\r\n");

                            break;
                        default:
                            break;
                    }
                    return sb.ToString();
                default:
                    break;
            }
            if (frame[analysisPoint++] == 0)
            {
                sb.Append("无跟随上报信息域\r\n");
            }
            else
            {
                sb.Append("有跟随上报信息域\r\n");
            }
            if (analysisPoint>= frame.Length)
            {
                sb.Append("解释时间标签有误！");
                return sb.ToString();
            }
            if (frame[analysisPoint++] == 0)
            {
                sb.Append("没有时间标签\r\n");
            }
            else
            {
                if (analysisPoint+6>= frame.Length)
                {
                    sb.Append("解释时间标签有误！");
                    return sb.ToString();
                }
                sb.Append("时间标签:\r\n");
                sb.Append($"发送时标{BitConverter.ToInt16(new byte[] { frame[analysisPoint+1], frame[analysisPoint] }, 0)}年{frame[analysisPoint + 2]}月{frame[analysisPoint + 3]}日");
                sb.Append($"{frame[analysisPoint+4]}时{frame[analysisPoint + 5]}分{frame[analysisPoint + 6]}秒\r\n");
                string unit = "";
                switch (frame[analysisPoint + 7])
                {
                    case 0:
                        unit = "秒";
                        break;
                    case 1:
                        unit = "分";
                        break;
                    case 2:
                        unit = "时";
                        break;
                    case 3:
                        unit = "日";
                        break;
                    case 4:
                        unit = "月";
                        break;
                    case 5:
                        unit = "年";
                        break;
                    default:
                        break;
                }
                sb.Append($"允许传输延时时间:{BitConverter.ToInt16(new byte[] { frame[analysisPoint+9], frame[analysisPoint + 8] }, 0)}{unit}\r\n");
            }
            return sb.ToString();
        }
        /*public static string AnalysisData(byte[] frame, string OIName, string OAD)
        {
            StringBuilder sb = new StringBuilder();
            
            int dataNum;
            string result = "";
            string oi;
            string temp = "";
            int data32 = 0;
            Int16 data16 = 0;
            Int64 data64 = 0;
            switch (frame[analysisPoint++])
            {
                case 0:

                    sb.Append("NULL\r\n"); ;
                    break;
                case 1:
                    
                    dataNum = frame[analysisPoint++];
                    if (1 == 0 && OIName.Contains("功电能") || OIName.Contains("需量"))
                    {
                        for (int i = 0; i < dataNum; i++)
                        {
                            //switch()
                            if (i == 0)
                            {
                                sb.Append("总：");
                            }
                            else if (i == 1)
                            {
                                sb.Append("费率1：");
                            }
                            else if (i == 2)
                            {
                                sb.Append("费率2：");
                            }
                            else if (i == 3)
                            {
                                sb.Append("费率3：");
                            }
                            else if (i == 4)
                            {
                                sb.Append("费率4：");
                            }
                            sb.Append(AnalysisData(frame, OIName, OAD) + "\r\n");
                        }
                    }
                    else
                    {
                        sb.Append($"{dataNum}个元素(array)\r\n");
                        for (int i = 0; i < dataNum; i++)
                        {
                            sb.Append($"array-{i + 1}\r\n");
                            sb.Append(AnalysisData(frame, OIName, OAD) + "\r\n");

                        }
                    }
                    break;
                case 2:
                    int structureNum = frame[analysisPoint++];
                    sb.Append($"{structureNum}个元素(structure)\r\n");
                    sb.Append("{");
                    for (int i = 0; i < structureNum; i++)
                    {
                        if (OAD.Equals("F1000700") && i == 0)
                        {
                            temp = AnalysisData(frame, OIName, OAD);
                            SendModule.ESAMCounter = temp;
                            sb.Append(temp + "\r\n");
                        }
                        else if (OAD.Equals("43000300"))
                        {
                            switch (i)
                            {
                                case 0:
                                    sb.Append("厂商代码:");
                                    break;
                                case 1:
                                    sb.Append("软件版本号:");
                                    break;
                                case 2:
                                    sb.Append("软件版本日期:");
                                    break;
                                case 3:
                                    sb.Append("硬件版本号:");
                                    break;
                                case 4:
                                    sb.Append("硬件版本日期:");
                                    break;
                                case 5:
                                    sb.Append("厂家扩展信息:");
                                    break;
                                default:
                                    break;
                            }
                            sb.Append(AnalysisData(frame, OIName, OAD) + "\r\n");
                        }
                        else
                        {
                            sb.Append(AnalysisData(frame, OIName, OAD) + "\r\n");
                        }
                    }
                    sb.Append("}\r\n");
                    break;
                case 4:
                    int bitNum = frame[analysisPoint++];
                    switch (bitNum / 8)
                    {
                        case 2:
                            data16 = BitConverter.ToInt16(new byte[] { frame[analysisPoint + 1], frame[analysisPoint] }, 0);
                            sb.Append($"0x{data16.ToString("X4")}");
                            analysisPoint += 2;
                            break;
                        case 4:
                            data32 = BitConverter.ToInt32(new byte[] { frame[analysisPoint + 3], frame[analysisPoint + 2], frame[analysisPoint + 1], frame[analysisPoint] }, 0);
                            sb.Append($"0x{data32.ToString("X8")}");
                            analysisPoint += 4;
                            break;
                        default:
                            break;
                    }
                    break;
                case 5:
                case 6:
                    string unit = "";

                    int demi = 0;

                    if (OIName.Contains("电能"))
                    {
                        if (OIName.Contains("无功"))
                        {
                            unit = "kvarh";
                        }
                        else if (OIName.Contains("有功"))
                        {
                            unit = "kWh";
                        }
                        demi = 2;
                    }
                    else if (OIName.Contains("需量"))
                    {
                        if (OIName.Contains("无功"))
                        {
                            unit = "kvar";
                        }
                        else if (OIName.Contains("有功"))
                        {
                            unit = "kw";
                        }
                        demi = 4;
                    }

                    data32 = BitConverter.ToInt32(new byte[] { frame[analysisPoint + 3], frame[analysisPoint + 2], frame[analysisPoint + 1], frame[analysisPoint] }, 0);
                    analysisPoint += 4;
                    switch (demi)
                    {
                        case 2:
                            result = string.Format("{0:f2}{1}", data32 / 100.0, unit);
                            break;
                        case 4:
                            result = string.Format("{0:f4}{1}", data32 / 10000.0, unit);
                            break;
                        default:
                            result = string.Format("{0}{1}", data32, unit);
                            break;
                    }

                    sb.Append(result);
                    break;
                case 9:
                    int byteNum = frame[analysisPoint++];
                    //int byteNum = 0;
                    if (byteNum == 0x82)
                    {
                        byteNum = BitConverter.ToInt16(new byte[] { frame[analysisPoint+1] , frame[analysisPoint] },0);
                        analysisPoint += 2;
                    }
                    temp = "";
                    for (int i = 0; i < byteNum; i++)
                    {
                        temp += frame[analysisPoint + i].ToString("X2");
                    }
                    if (OAD.Equals("F1000200"))
                    {
                        //AppControlViewModel.ESAMDiv = temp;
                        SendModule.ESAMDiv=temp;
                    }
                    else if (OAD.Equals("F1000400"))
                    {
                        //AppControlViewModel.ESAMVersion = temp;
                        SendModule.ESAMVersion=temp;
                    }
                    else if (OAD.Equals("F1000A00"))
                    {
                        //AppControlViewModel.TerminalCert = temp;
                        SendModule.TerminalCert=temp;
                    }
                    else if (OAD.Equals("F1000C00"))
                    {
                        //AppControlViewModel.MasterCert = temp;
                        SendModule.MasterCert = temp;
                    }
                    result = */
        /*"0x" +*//* temp;
                    
                    analysisPoint += byteNum;
                    sb.Append(result); ;
                    break;
                case 3:
                case 0x0a:
                    int asciiNum = frame[analysisPoint++];

                    for (int i = 0; i < asciiNum; i++)
                    {
                        result += (char)frame[analysisPoint + i];

                    }
                    //result=StringUtils.asciiToString(temp);
                    sb.Append(result); ;
                    analysisPoint += asciiNum;
                    break;
                case 0x11:
                    sb.Append(frame[analysisPoint].ToString());
                    analysisPoint += 1;
                    break;
                case 0x10:
                case 0x12:
                    data16 = BitConverter.ToInt16(new byte[] { frame[analysisPoint + 1], frame[analysisPoint] }, 0);
                    analysisPoint += 2;
                    sb.Append(data16.ToString()); ;
                    break;
                case 0x14:
                case 0x15:
                    unit = "";

                    demi = 0;

                    if (OIName.Contains("电能"))
                    {
                        if (OIName.Contains("无功"))
                        {
                            unit = "kvarh";
                        }
                        else if (OIName.Contains("有功"))
                        {
                            unit = "kWh";
                        }
                        demi = 2;
                    }
                    else if (OIName.Contains("需量"))
                    {
                        if (OIName.Contains("无功"))
                        {
                            unit = "kvar";
                        }
                        else if (OIName.Contains("有功"))
                        {
                            unit = "kw";
                        }
                        demi = 4;
                    }
                    data64 = BitConverter.ToInt64(new byte[] { frame[analysisPoint + 7], frame[analysisPoint + 6], frame[analysisPoint + 5], frame[analysisPoint + 4], frame[analysisPoint + 3], frame[analysisPoint + 2], frame[analysisPoint + 1], frame[analysisPoint] }, 0);
                    analysisPoint += 8;
                    result = string.Format("{0:f4}{1}", data64 / 10000.0, unit);
                    sb.Append(result);
                    break;
                case 0x16:
                    //analysisPoint += 1;
                    sb.Append(frame[analysisPoint++].ToString()); ;
                    break;
                case 0x1c:
                    byte[] byteYear = new byte[2];
                    byteYear[0] = frame[analysisPoint];
                    byteYear[1] = frame[analysisPoint + 1];
                    short year = ByteUtils.hBytesToShort(byteYear);
                    string dateTime = string.Format("{0:d4}年{1:d2}月{2:d2}日{3:d2}时{4:d2}分{5:d2}秒", year, frame[analysisPoint + 2], frame[analysisPoint + 3],
                        frame[analysisPoint + 4], frame[analysisPoint + 5], frame[analysisPoint + 6]);
                    analysisPoint += 7;
                    sb.Append(dateTime.ToString());
                    break;
                case 0x50:
                    sb.Append($"对象标识：{frame[analysisPoint].ToString("X2") + frame[analysisPoint + 1].ToString("X2")}");
                    analysisPoint += 2;
                    break;
                case 0x51:
                    oi = frame[analysisPoint].ToString("X2") + frame[analysisPoint + 1].ToString("X2");
                    sb.Append($"对象属性描述符：{oi + frame[analysisPoint + 2].ToString("X2") + frame[analysisPoint + 3].ToString("X2")}——{oIService.SelectNameByOI(oi).MName}");
                    analysisPoint += 4;
                    break;
                case 0x55:
                    //TSA
                    sb.Append("目标服务器地址TSA:");
                    for (int i = 0; i < frame[analysisPoint+1]+1; i++)
                    {
                        sb.Append(frame[analysisPoint + 1+i+1].ToString("X2"));
                    }
                    analysisPoint += frame[analysisPoint]+1;
                    break;
                case 0x59:
                    string s = frame[analysisPoint++].ToString("X2");
                    var d = Convert.ToSByte(s, 16);
                    sb.Append("换算：" + d.ToString() + "\r\n");
                    sb.Append("单位：" + enumInfoService.SelectUnit(Convert.ToSByte(frame[analysisPoint++].ToString("X2"), 16)));
                    break;
                case 0x5B:

                    oi = frame[analysisPoint + 1].ToString("X2") + frame[analysisPoint + 2].ToString("X2");
                    sb.Append($"对象属性描述符：{oi + frame[analysisPoint + 3].ToString("X2") + frame[analysisPoint + 4].ToString("X2")}——{oIService.SelectNameByOI(oi).MName}");
                    analysisPoint += 5;

                    break;
                case 0x5f:
                    for (int i = 0; i < 5 ; i++)
                    {
                        sb.Append(frame[analysisPoint + i].ToString(""));
                        if (i+1<5)
                        {
                            sb.Append(",");
                        }
                        
                    }
                    
                    analysisPoint += 5;
                    break;
                default:
                    break;
            }
            return sb.ToString();
        }*/
        /*public static List<List<string>> AnalysisData(byte[] frame, string OIName, string OAD)
        {
            //StringBuilder sb = new StringBuilder();
            List<List<string>> list = new List<List<string>>();
            List<string> tempList = new List<string>();
            int dataNum;
            string result = "";
            string oi;
            string temp = "";
            int data32 = 0;
            Int16 data16 = 0;
            Int64 data64 = 0;
            switch (frame[analysisPoint++])
            {
                case 0:
                    //list.Add("NULL");
                    //tempList.Add("NULL");
                    list.Add(tempList);
                    //sb.Append("NULL\r\n"); ;
                    break;
                case 1:

                    dataNum = frame[analysisPoint++];
                    if (dataNum<=0)
                    {
                        list.Add(tempList);
                    }
                    // temp = "";
                    for (int i = 0; i < dataNum; i++)
                    {
                        tempList = new List<string>();
                        //sb.Append($"array-{i + 1}\r\n");
                        //sb.Append(AnalysisData(frame, OIName, OAD) + "\r\n");
                        //list.AddRange(AnalysisData(frame, OIName, OAD));
                        *//* var tempList = AnalysisData(frame, OIName, OAD);
                         for (int j = 0; j < tempList.Count; j++)
                         {
                             temp += tempList[j] + ";";
                         }*//*
                        //temp += AnalysisData(frame, OIName, OAD)[0]+",";
                        var tempLists = AnalysisData(frame, OIName, OAD);
                        for (int j = 0; j < tempLists.Count; j++)
                        {
                            tempList.Add(tempLists[j][0]);
                        }
                        list.Add(tempList);
                    }
                    
                    //temp=temp.TrimEnd(';');
                    //list.Add(temp);
                    break;
                case 2:
                    int structureNum = frame[analysisPoint++];
                    //sb.Append($"{structureNum}个元素(structure)\r\n");
                    //sb.Append("{");
                    temp = "";
                    for (int i = 0; i < structureNum; i++)
                    {
                        var tempLists = AnalysisData(frame, OIName, OAD);
                        tempList = new List<string>();
                        if (OAD.Equals("F1000700") && i == 0)
                        {
                            
                            SendModule.ESAMCounter = tempLists[0][0];
                            
                            tempList.Add(tempLists[0][0]);
                           // temp += tempList + ",";
                            //sb.Append(temp + "\r\n");
                        }
                        *//*else if (OAD.Equals("43000300"))
                        {
                            switch (i)
                            {
                                case 0:
                                    sb.Append("厂商代码:");
                                    break;
                                case 1:
                                    sb.Append("软件版本号:");
                                    break;
                                case 2:
                                    sb.Append("软件版本日期:");
                                    break;
                                case 3:
                                    sb.Append("硬件版本号:");
                                    break;
                                case 4:
                                    sb.Append("硬件版本日期:");
                                    break;
                                case 5:
                                    sb.Append("厂家扩展信息:");
                                    break;
                                default:
                                    break;
                            }
                            sb.Append(AnalysisData(frame, OIName, OAD) + "\r\n");
                        }*//*
                        else
                        {
                            //sb.Append(AnalysisData(frame, OIName, OAD) + "\r\n");
                            for (int j = 0; j < tempLists.Count; j++)
                            {
                                if (tempLists[j].Count>0)
                                {
                                    tempList.Add(tempLists[j][0]);
                                }
                                else
                                {
                                    tempList.Add(null);
                                }
                                
                            }
                            //temp += AnalysisData(frame, OIName, OAD)[0] + ",";
                        }
                        list.Add(tempList);
                    }

                    
                    //temp = temp.TrimEnd(',');
                    //list.Add(temp);
                    // sb.Append("}\r\n");
                    break;
                case 4:
                    int bitNum = frame[analysisPoint++];
                    switch (bitNum / 8)
                    {
                        case 1:
                            tempList.Add(frame[analysisPoint].ToString("X2"));
                            list.Add(tempList);
                            //list.Add(frame[analysisPoint].ToString("X2"));
                            //sb.Append($"0x{data16.ToString("X4")}");
                            analysisPoint += 1;
                            break;
                        case 2:
                            data16 = BitConverter.ToInt16(new byte[] { frame[analysisPoint + 1], frame[analysisPoint] }, 0);
                            //list.Add(data16.ToString("X4"));
                            tempList.Add(data16.ToString("X4"));
                            list.Add(tempList);
                            //sb.Append($"0x{data16.ToString("X4")}");
                            analysisPoint += 2;
                            break;
                        case 4:
                            data32 = BitConverter.ToInt32(new byte[] { frame[analysisPoint + 3], frame[analysisPoint + 2], frame[analysisPoint + 1], frame[analysisPoint] }, 0);
                            //sb.Append($"0x{data32.ToString("X8")}");
                            tempList.Add(data32.ToString("X8"));
                            list.Add(tempList);
                            //list.Add(data32.ToString("X8"));
                            analysisPoint += 4;
                            break;
                        default:
                            break;
                    }
                    break;
                case 5:
                case 6:
                    string unit = "";

                    int demi = 0;

                    if (OIName.Contains("电能"))
                    {
                        if (OIName.Contains("无功"))
                        {
                            unit = "kvarh";
                        }
                        else if (OIName.Contains("有功"))
                        {
                            unit = "kWh";
                        }
                        demi = 2;
                    }
                    else if (OIName.Contains("需量"))
                    {
                        if (OIName.Contains("无功"))
                        {
                            unit = "kvar";
                        }
                        else if (OIName.Contains("有功"))
                        {
                            unit = "kw";
                        }
                        demi = 4;
                    }

                    data32 = BitConverter.ToInt32(new byte[] { frame[analysisPoint + 3], frame[analysisPoint + 2], frame[analysisPoint + 1], frame[analysisPoint] }, 0);
                    analysisPoint += 4;
                    switch (demi)
                    {
                        case 2:
                            result = string.Format("{0:f2}{1}", data32 / 100.0, unit);
                            break;
                        case 4:
                            result = string.Format("{0:f4}{1}", data32 / 10000.0, unit);
                            break;
                        default:
                            result = string.Format("{0}{1}", data32, unit);
                            break;
                    }
                    //list.Add(result);
                    tempList.Add(result);
                    list.Add(tempList);
                    //sb.Append(result);
                    break;
                case 9:
                    int byteNum = frame[analysisPoint++];
                    //int byteNum = 0;
                    if (byteNum == 0x82)
                    {
                        byteNum = BitConverter.ToInt16(new byte[] { frame[analysisPoint + 1], frame[analysisPoint] }, 0);
                        analysisPoint += 2;
                    }
                    temp = "";
                    for (int i = 0; i < byteNum; i++)
                    {
                        temp += frame[analysisPoint + i].ToString("X2");
                    }
                    if (OAD.Equals("F1000200"))
                    {
                        //AppControlViewModel.ESAMDiv = temp;
                        SendModule.ESAMDiv = temp;
                    }
                    else if (OAD.Equals("F1000400"))
                    {
                        //AppControlViewModel.ESAMVersion = temp;
                        SendModule.ESAMVersion = temp;
                    }
                    else if (OAD.Equals("F1000A00"))
                    {
                        //AppControlViewModel.TerminalCert = temp;
                        SendModule.TerminalCert = temp;
                    }
                    else if (OAD.Equals("F1000C00"))
                    {
                        //AppControlViewModel.MasterCert = temp;
                        SendModule.MasterCert = temp;
                    }
                    result = *//*"0x" +*//* temp;

                    analysisPoint += byteNum;
                    //list.Add(result);
                    tempList.Add(result);
                    list.Add(tempList);
                    // sb.Append(result); ;
                    break;
                case 3:
                    tempList.Add(frame[analysisPoint].ToString());
                    list.Add(tempList);
                    analysisPoint += 1;
                    break;
                case 0x0a:
                    int asciiNum = frame[analysisPoint++];

                    for (int i = 0; i < asciiNum; i++)
                    {
                        result += (char)frame[analysisPoint + i];

                    }
                    //result=StringUtils.asciiToString(temp);
                    // sb.Append(result); ;
                    //list.Add(result);
                    tempList.Add(result);
                    list.Add(tempList);
                    analysisPoint += asciiNum;
                    break;
                case 0x11:
                    // sb.Append(frame[analysisPoint].ToString());
                    tempList.Add(frame[analysisPoint].ToString());
                    list.Add(tempList);
                    //list.Add(frame[analysisPoint].ToString());
                    analysisPoint += 1;
                    break;
                case 0x10:
                case 0x12:
                    data16 = BitConverter.ToInt16(new byte[] { frame[analysisPoint + 1], frame[analysisPoint] }, 0);
                    analysisPoint += 2;
                    //list.Add(data16.ToString());
                    tempList.Add(data16.ToString());
                    list.Add(tempList);
                    // sb.Append(data16.ToString()); ;
                    break;
                case 0x14:
                case 0x15:
                    unit = "";

                    demi = 0;

                    if (OIName.Contains("电能"))
                    {
                        if (OIName.Contains("无功"))
                        {
                            unit = "kvarh";
                        }
                        else if (OIName.Contains("有功"))
                        {
                            unit = "kWh";
                        }
                        demi = 2;
                    }
                    else if (OIName.Contains("需量"))
                    {
                        if (OIName.Contains("无功"))
                        {
                            unit = "kvar";
                        }
                        else if (OIName.Contains("有功"))
                        {
                            unit = "kw";
                        }
                        demi = 4;
                    }
                    data64 = BitConverter.ToInt64(new byte[] { frame[analysisPoint + 7], frame[analysisPoint + 6], frame[analysisPoint + 5], frame[analysisPoint + 4], frame[analysisPoint + 3], frame[analysisPoint + 2], frame[analysisPoint + 1], frame[analysisPoint] }, 0);
                    analysisPoint += 8;
                    result = string.Format("{0:f4}{1}", data64 / 10000.0, unit);
                    // sb.Append(result);
                    tempList.Add(result);
                    list.Add(tempList);
                    //list.Add(result);
                    break;
                case 0x16:
                    //analysisPoint += 1;
                    // sb.Append(frame[analysisPoint++].ToString()); ;
                    tempList.Add(frame[analysisPoint++].ToString());
                    list.Add(tempList);
                    //list.Add(frame[analysisPoint++].ToString());
                    break;
                case 0x1c:
                    byte[] byteYear = new byte[2];
                    byteYear[0] = frame[analysisPoint];
                    byteYear[1] = frame[analysisPoint + 1];
                    short year = ByteUtils.hBytesToShort(byteYear);
                    string dateTime = string.Format("{0:d4}年{1:d2}月{2:d2}日{3:d2}时{4:d2}分{5:d2}秒", year, frame[analysisPoint + 2], frame[analysisPoint + 3],
                        frame[analysisPoint + 4], frame[analysisPoint + 5], frame[analysisPoint + 6]);
                    analysisPoint += 7;
                    //  sb.Append(dateTime.ToString());
                    tempList.Add(dateTime.ToString());
                    list.Add(tempList);
                    //list.Add(dateTime.ToString());
                    break;
                case 0x50:
                    //sb.Append($"对象标识：{frame[analysisPoint].ToString("X2") + frame[analysisPoint + 1].ToString("X2")}");
                    //list.Add(frame[analysisPoint].ToString("X2") + frame[analysisPoint + 1].ToString("X2"));
                    //list.Add($"对象标识：{frame[analysisPoint].ToString("X2") + frame[analysisPoint + 1].ToString("X2")}");
                    tempList.Add($"对象标识：{frame[analysisPoint].ToString("X2") + frame[analysisPoint + 1].ToString("X2")}");
                    list.Add(tempList);
                    analysisPoint += 2;
                    break;
                case 0x51:
                    oi = frame[analysisPoint].ToString("X2") + frame[analysisPoint + 1].ToString("X2");
                    //  sb.Append($"对象属性描述符：{oi + frame[analysisPoint + 2].ToString("X2") + frame[analysisPoint + 3].ToString("X2")}——{oIService.SelectNameByOI(oi).MName}");
                    //list.Add(oi + frame[analysisPoint + 2].ToString("X2") + frame[analysisPoint + 3].ToString("X2"));
                    tempList.Add($"对象属性描述符：{oi + frame[analysisPoint + 2].ToString("X2") + frame[analysisPoint + 3].ToString("X2")}——{oIService.SelectNameByOI(oi).MName}");
                    list.Add(tempList);
                    //list.Add($"对象属性描述符：{oi + frame[analysisPoint + 2].ToString("X2") + frame[analysisPoint + 3].ToString("X2")}——{oIService.SelectNameByOI(oi).MName}");
                    analysisPoint += 4;
                    break;
                case 0x55:
                    //TSA
                    //  sb.Append("目标服务器地址TSA:");
                    temp = "";
                    for (int i = 0; i < frame[analysisPoint + 1] + 1; i++)
                    {
                        //sb.Append(frame[analysisPoint + 1 + i + 1].ToString("X2"));
                        temp += frame[analysisPoint + 1 + i + 1].ToString("X2");
                        //list.Add(frame[analysisPoint + 1 + i + 1].ToString("X2"));
                    }
                    //list.Add("目标服务器地址TSA:" + temp);
                    tempList.Add("目标服务器地址TSA:" + temp);
                    list.Add(tempList);
                    analysisPoint += frame[analysisPoint] + 1;
                    break;
                case 0x59:
                    string s = frame[analysisPoint++].ToString("X2");
                    var d = Convert.ToSByte(s, 16);
                    //  sb.Append("换算：" + d.ToString() + "\r\n");
                    //  sb.Append("单位：" + enumInfoService.SelectUnit(Convert.ToSByte(frame[analysisPoint++].ToString("X2"), 16)));
                    //list.Add("换算：" + d.ToString() + "\r\n"+ "单位：" + enumInfoService.SelectUnit(Convert.ToSByte(frame[analysisPoint++].ToString("X2"), 16)));
                    tempList.Add("换算：" + d.ToString() + "\r\n" + "单位：" + enumInfoService.SelectUnit(Convert.ToSByte(frame[analysisPoint++].ToString("X2"), 16)));
                    list.Add(tempList);
                    break;
                case 0x5B:

                    oi = frame[analysisPoint + 1].ToString("X2") + frame[analysisPoint + 2].ToString("X2");
                    //  sb.Append($"对象属性描述符：{oi + frame[analysisPoint + 3].ToString("X2") + frame[analysisPoint + 4].ToString("X2")}——{oIService.SelectNameByOI(oi).MName}");
                    tempList.Add(oi + frame[analysisPoint + 3].ToString("X2") + frame[analysisPoint + 4].ToString("X2"));
                    list.Add(tempList);
                    //list.Add(oi + frame[analysisPoint + 3].ToString("X2") + frame[analysisPoint + 4].ToString("X2"));
                    analysisPoint += 5;

                    break;
                case 0x5f:
                    temp = "";
                    for (int i = 0; i < 5; i++)
                    {
                        if (i==2)
                        {
                            temp += (frame[analysisPoint + i]-5).ToString("");
                        }
                        else if (i==3)
                        {
                            temp += (frame[analysisPoint + i] - 1).ToString("");
                        }
                        else
                        {
                            temp += frame[analysisPoint + i].ToString("");
                        }
                        
                        if (i + 1 < 5)
                        {
                            temp += ",";
                        }
                        *//*sb.Append(frame[analysisPoint + i].ToString(""));
                        if (i + 1 < 5)
                        {
                            sb.Append(",");
                        }*//*

                    }
                    //list.Add(temp);
                    tempList.Add(temp);
                    list.Add(tempList);
                    analysisPoint += 5;
                    break;
                default:
                    break;
            }
            return list;
        }*/
        public static List<object> AnalysisData(byte[] frame, string OIName, string OAD)
        {
            
            //StringBuilder sb = new StringBuilder();
            List<object> list = new List<object>();
            List<object> tempList = new List<object>();
            List<object> tempList1 = new List<object>();
            
            int dataNum;
            string result = "";
            string oi;
            string temp = "";
            int data32 = 0;
            Int16 data16 = 0;
            Int64 data64 = 0;
            int num = 0;
            switch (frame[analysisPoint++])
            {
                case 0:
                    //list.Add("NULL");
                    //tempList.Add("NULL");
                    //list.Add(null);
                    //sb.Append("NULL\r\n"); ;
                    break;
                case 1:

                    dataNum = frame[analysisPoint++];
                    if (dataNum <= 0)
                    {
                        //list.Add(null);
                    }
                    // temp = "";
                    for (int i = 0; i < dataNum; i++)
                    {
                        //tempList = new List<string>();
                        //sb.Append($"array-{i + 1}\r\n");
                        //sb.Append(AnalysisData(frame, OIName, OAD) + "\r\n");
                        //list.AddRange(AnalysisData(frame, OIName, OAD));
                        /* var tempList = AnalysisData(frame, OIName, OAD);
                         for (int j = 0; j < tempList.Count; j++)
                         {
                             temp += tempList[j] + ";";
                         }*/
                        //temp += AnalysisData(frame, OIName, OAD)[0]+",";
                        list.Add(AnalysisData(frame, OIName, OAD));
                    }

                    //temp=temp.TrimEnd(';');
                    //list.Add(temp);
                    break;
                case 2:
                    int structureNum = frame[analysisPoint++];
                    //sb.Append($"{structureNum}个元素(structure)\r\n");
                    //sb.Append("{");
                    temp = "";
                    for (int i = 0; i < structureNum; i++)
                    {
                        var tempLists = AnalysisData(frame, OIName, OAD);
                        //tempList = new List<string>();
                        if (OAD.Equals("F1000700") && i == 0)
                        {

                            SendModule.ESAMCounter = (string)tempLists[0];

                            //tempList.Add(tempLists[0][0]);
                            // temp += tempList + ",";
                            //sb.Append(temp + "\r\n");
                        }
                        /*else if (OAD.Equals("43000300"))
                        {
                            switch (i)
                            {
                                case 0:
                                    sb.Append("厂商代码:");
                                    break;
                                case 1:
                                    sb.Append("软件版本号:");
                                    break;
                                case 2:
                                    sb.Append("软件版本日期:");
                                    break;
                                case 3:
                                    sb.Append("硬件版本号:");
                                    break;
                                case 4:
                                    sb.Append("硬件版本日期:");
                                    break;
                                case 5:
                                    sb.Append("厂家扩展信息:");
                                    break;
                                default:
                                    break;
                            }
                            sb.Append(AnalysisData(frame, OIName, OAD) + "\r\n");
                        }*/
                        else
                        {
                            //sb.Append(AnalysisData(frame, OIName, OAD) + "\r\n");
                            /*for (int j = 0; j < tempLists.Count; j++)
                            {
                                if (tempLists[j].Count > 0)
                                {
                                    tempList.Add(tempLists[j][0]);
                                }
                                else
                                {
                                    tempList.Add(null);
                                }

                            }*/
                            //temp += AnalysisData(frame, OIName, OAD)[0] + ",";
                        }
                        //list.Add(tempList);
                        list.Add(tempLists);
                    }


                    //temp = temp.TrimEnd(',');
                    //list.Add(temp);
                    // sb.Append("}\r\n");
                    break;
                case 4:
                    int bitNum = frame[analysisPoint++];
                    switch (bitNum / 8)
                    {
                        case 1:
                            list.Add(frame[analysisPoint].ToString("X2"));
                            
                            //list.Add(frame[analysisPoint].ToString("X2"));
                            //sb.Append($"0x{data16.ToString("X4")}");
                            analysisPoint += 1;
                            break;
                        case 2:
                            data16 = BitConverter.ToInt16(new byte[] { frame[analysisPoint + 1], frame[analysisPoint] }, 0);
                            //list.Add(data16.ToString("X4"));
                            list.Add(data16.ToString("X4"));
                            
                            //sb.Append($"0x{data16.ToString("X4")}");
                            analysisPoint += 2;
                            break;
                        case 4:
                            data32 = BitConverter.ToInt32(new byte[] { frame[analysisPoint + 3], frame[analysisPoint + 2], frame[analysisPoint + 1], frame[analysisPoint] }, 0);
                            //sb.Append($"0x{data32.ToString("X8")}");
                            list.Add(data32.ToString("X8"));
                            
                            //list.Add(data32.ToString("X8"));
                            analysisPoint += 4;
                            break;
                        default:
                            break;
                    }
                    break;
                case 5:
                case 6:
                    string unit = "";

                    int demi = 0;

                    if (OIName.Contains("电能"))
                    {
                        if (OIName.Contains("无功"))
                        {
                            unit = "kvarh";
                        }
                        else if (OIName.Contains("有功"))
                        {
                            unit = "kWh";
                        }
                        demi = 2;
                    }
                    else if (OIName.Contains("需量"))
                    {
                        if (OIName.Contains("无功"))
                        {
                            unit = "kvar";
                        }
                        else if (OIName.Contains("有功"))
                        {
                            unit = "kw";
                        }
                        demi = 4;
                    }

                    data32 = BitConverter.ToInt32(new byte[] { frame[analysisPoint + 3], frame[analysisPoint + 2], frame[analysisPoint + 1], frame[analysisPoint] }, 0);
                    analysisPoint += 4;
                    switch (demi)
                    {
                        case 2:
                            result = string.Format("{0:f2}{1}", data32 / 100.0, unit);
                            break;
                        case 4:
                            result = string.Format("{0:f4}{1}", data32 / 10000.0, unit);
                            break;
                        default:
                            result = string.Format("{0}{1}", data32, unit);
                            break;
                    }
                    //list.Add(result);
                    list.Add(result);
                    
                    //sb.Append(result);
                    break;
                case 9:
                    int byteNum = frame[analysisPoint++];
                    //int byteNum = 0;
                    if (byteNum == 0x82)
                    {
                        byteNum = BitConverter.ToInt16(new byte[] { frame[analysisPoint + 1], frame[analysisPoint] }, 0);
                        analysisPoint += 2;
                    }
                    temp = "";
                    for (int i = 0; i < byteNum; i++)
                    {
                        temp += frame[analysisPoint + i].ToString("X2");
                    }
                    if (OAD.Equals("F1000200"))
                    {
                        //AppControlViewModel.ESAMDiv = temp;
                        SendModule.ESAMDiv = temp;
                    }
                    else if (OAD.Equals("F1000400"))
                    {
                        //AppControlViewModel.ESAMVersion = temp;
                        SendModule.ESAMVersion = temp;
                    }
                    else if (OAD.Equals("F1000A00"))
                    {
                        //AppControlViewModel.TerminalCert = temp;
                        SendModule.TerminalCert = temp;
                    }
                    else if (OAD.Equals("F1000C00"))
                    {
                        //AppControlViewModel.MasterCert = temp;
                        SendModule.MasterCert = temp;
                    }
                    result = /*"0x" +*/ temp;

                    analysisPoint += byteNum;
                    //list.Add(result);
                    list.Add(result);
                   
                    // sb.Append(result); ;
                    break;
                case 3:
                    list.Add(frame[analysisPoint].ToString());
                   
                    analysisPoint += 1;
                    break;
                case 0x0a:
                    int asciiNum = frame[analysisPoint++];

                    for (int i = 0; i < asciiNum; i++)
                    {
                        result += (char)frame[analysisPoint + i];

                    }
                    //result=StringUtils.asciiToString(temp);
                    // sb.Append(result); ;
                    //list.Add(result);
                    list.Add(result);
                    
                    analysisPoint += asciiNum;
                    break;
                case 0x11:
                    // sb.Append(frame[analysisPoint].ToString());
                    list.Add(frame[analysisPoint].ToString());
                   
                    //list.Add(frame[analysisPoint].ToString());
                    analysisPoint += 1;
                    break;
                case 0x10:
                case 0x12:
                    data16 = BitConverter.ToInt16(new byte[] { frame[analysisPoint + 1], frame[analysisPoint] }, 0);
                    analysisPoint += 2;
                    //list.Add(data16.ToString());
                    list.Add(data16.ToString());
                    
                    // sb.Append(data16.ToString()); ;
                    break;
                case 0x14:
                case 0x15:
                    unit = "";

                    demi = 0;

                    if (OIName.Contains("电能"))
                    {
                        if (OIName.Contains("无功"))
                        {
                            unit = "kvarh";
                        }
                        else if (OIName.Contains("有功"))
                        {
                            unit = "kWh";
                        }
                        demi = 2;
                    }
                    else if (OIName.Contains("需量"))
                    {
                        if (OIName.Contains("无功"))
                        {
                            unit = "kvar";
                        }
                        else if (OIName.Contains("有功"))
                        {
                            unit = "kw";
                        }
                        demi = 4;
                    }
                    data64 = BitConverter.ToInt64(new byte[] { frame[analysisPoint + 7], frame[analysisPoint + 6], frame[analysisPoint + 5], frame[analysisPoint + 4], frame[analysisPoint + 3], frame[analysisPoint + 2], frame[analysisPoint + 1], frame[analysisPoint] }, 0);
                    analysisPoint += 8;
                    result = string.Format("{0:f4}{1}", data64 / 10000.0, unit);
                    // sb.Append(result);
                    list.Add(result);
                   
                    //list.Add(result);
                    break;
                case 0x16:
                    //analysisPoint += 1;
                    // sb.Append(frame[analysisPoint++].ToString()); ;
                    list.Add(frame[analysisPoint++].ToString());
                   
                    //list.Add(frame[analysisPoint++].ToString());
                    break;
                case 0x1c:
                    byte[] byteYear = new byte[2];
                    byteYear[0] = frame[analysisPoint];
                    byteYear[1] = frame[analysisPoint + 1];
                    short year = ByteUtils.hBytesToShort(byteYear);
                    string dateTime = string.Format("{0:d4}年{1:d2}月{2:d2}日{3:d2}时{4:d2}分{5:d2}秒", year, frame[analysisPoint + 2], frame[analysisPoint + 3],
                        frame[analysisPoint + 4], frame[analysisPoint + 5], frame[analysisPoint + 6]);
                    analysisPoint += 7;
                    //  sb.Append(dateTime.ToString());
                    list.Add(dateTime.ToString());
                    
                    //list.Add(dateTime.ToString());
                    break;
                case 0x50:
                    //sb.Append($"对象标识：{frame[analysisPoint].ToString("X2") + frame[analysisPoint + 1].ToString("X2")}");
                    //list.Add(frame[analysisPoint].ToString("X2") + frame[analysisPoint + 1].ToString("X2"));
                    //list.Add($"对象标识：{frame[analysisPoint].ToString("X2") + frame[analysisPoint + 1].ToString("X2")}");
                    list.Add($"对象标识：{frame[analysisPoint].ToString("X2") + frame[analysisPoint + 1].ToString("X2")}");
                    
                    analysisPoint += 2;
                    break;
                case 0x51:
                    oi = frame[analysisPoint].ToString("X2") + frame[analysisPoint + 1].ToString("X2");
                    //  sb.Append($"对象属性描述符：{oi + frame[analysisPoint + 2].ToString("X2") + frame[analysisPoint + 3].ToString("X2")}——{oIService.SelectNameByOI(oi).MName}");
                    //list.Add(oi + frame[analysisPoint + 2].ToString("X2") + frame[analysisPoint + 3].ToString("X2"));
                    //list.Add($"对象属性描述符：{oi + frame[analysisPoint + 2].ToString("X2") + frame[analysisPoint + 3].ToString("X2")}——{oIService.SelectNameByOI(oi).MName}");
                    list.Add($"{oi + frame[analysisPoint + 2].ToString("X2") + frame[analysisPoint + 3].ToString("X2")}");
                    //list.Add($"对象属性描述符：{oi + frame[analysisPoint + 2].ToString("X2") + frame[analysisPoint + 3].ToString("X2")}——{oIService.SelectNameByOI(oi).MName}");
                    analysisPoint += 4;
                    break;
                case 0x54:
                    //temp = frame[analysisPoint++].ToString("X2");
                    temp = Enums.GetTIUnit(frame[analysisPoint++]);
                    data16 = BitConverter.ToInt16(new byte[] { frame[analysisPoint + 1], frame[analysisPoint] }, 0);
                    analysisPoint += 2;
                    list.Add(data16.ToString()+temp);
                    //list.Add(temp+data16.ToString("X4"));
                    break;
                case 0x55:
                    //TSA
                    //  sb.Append("目标服务器地址TSA:");
                    temp = "";
                    for (int i = 0; i < frame[analysisPoint + 1] + 1; i++)
                    {
                        //sb.Append(frame[analysisPoint + 1 + i + 1].ToString("X2"));
                        temp += frame[analysisPoint + 1 + i + 1].ToString("X2");
                        //list.Add(frame[analysisPoint + 1 + i + 1].ToString("X2"));
                    }
                    //list.Add("目标服务器地址TSA:" + temp);
                    //list.Add("目标服务器地址TSA:" + temp);
                    list.Add(temp);
                    analysisPoint += frame[analysisPoint] + 1;
                    break;
                case 0x59:
                    string s = frame[analysisPoint++].ToString("X2");
                    var d = Convert.ToSByte(s, 16);
                    //  sb.Append("换算：" + d.ToString() + "\r\n");
                    //  sb.Append("单位：" + enumInfoService.SelectUnit(Convert.ToSByte(frame[analysisPoint++].ToString("X2"), 16)));
                    //list.Add("换算：" + d.ToString() + "\r\n"+ "单位：" + enumInfoService.SelectUnit(Convert.ToSByte(frame[analysisPoint++].ToString("X2"), 16)));
                    list.Add("换算：" + d.ToString() + "\r\n" + "单位：" + enumInfoService.SelectUnit(Convert.ToSByte(frame[analysisPoint++].ToString("X2"), 16)));
                    
                    break;
                case 0x5B:
                    var csdIndex = frame[analysisPoint++];
                    switch (csdIndex)
                    {
                        case 0:
                            oi = frame[analysisPoint ].ToString("X2") + frame[analysisPoint + 1].ToString("X2");
                            //  sb.Append($"对象属性描述符：{oi + frame[analysisPoint + 3].ToString("X2") + frame[analysisPoint + 4].ToString("X2")}——{oIService.SelectNameByOI(oi).MName}");
                            list.Add(oi + frame[analysisPoint + 2].ToString("X2") + frame[analysisPoint + 3].ToString("X2"));

                            //list.Add(oi + frame[analysisPoint + 3].ToString("X2") + frame[analysisPoint + 4].ToString("X2"));
                            analysisPoint += 4;
                            break;
                        case 1:
                            List<object> rcsdList = new List<object>();
                            oi = frame[analysisPoint].ToString("X2") + frame[analysisPoint + 1].ToString("X2");
                            //  sb.Append($"对象属性描述符：{oi + frame[analysisPoint + 3].ToString("X2") + frame[analysisPoint + 4].ToString("X2")}——{oIService.SelectNameByOI(oi).MName}");
                            //list.Add(oi + frame[analysisPoint + 2].ToString("X2") + frame[analysisPoint + 3].ToString("X2"));
                            rcsdList.Add(oi + frame[analysisPoint + 2].ToString("X2") + frame[analysisPoint + 3].ToString("X2"));
                            //list.Add(oi + frame[analysisPoint + 3].ToString("X2") + frame[analysisPoint + 4].ToString("X2"));
                            analysisPoint += 4;
                            num = frame[analysisPoint++];
                            for (int i = 0; i < num; i++)
                            {
                                oi = frame[analysisPoint].ToString("X2") + frame[analysisPoint + 1].ToString("X2");
                                //  sb.Append($"对象属性描述符：{oi + frame[analysisPoint + 3].ToString("X2") + frame[analysisPoint + 4].ToString("X2")}——{oIService.SelectNameByOI(oi).MName}");
                                //list.Add(oi + frame[analysisPoint + 2].ToString("X2") + frame[analysisPoint + 3].ToString("X2"));
                                rcsdList.Add(oi + frame[analysisPoint + 2].ToString("X2") + frame[analysisPoint + 3].ToString("X2"));
                                //list.Add(oi + frame[analysisPoint + 3].ToString("X2") + frame[analysisPoint + 4].ToString("X2"));
                                analysisPoint += 4;
                            }
                            list.Add(rcsdList);
                            break;
                        default:
                            break;
                    }
                    

                    break;
                case 0x5c:
                    var MSIndex = frame[analysisPoint++];
                    temp = MSIndex.ToString("X2");
                    switch (MSIndex)
                    {
                        case 0:
                        case 1:
                            break;
                        case 2:
                            num = frame[analysisPoint++];
                            temp += num.ToString("X2");
                            for (int i = 0; i < num; i++)
                            {
                                temp += frame[analysisPoint++].ToString("X2");
                            }
                            break;
                        case 3:
                            num = frame[analysisPoint++];
                            temp += num.ToString("X2");
                            for (int i = 0; i < num; i++)
                            {
                                temp += frame[analysisPoint].ToString("X2");
                                temp += frame[analysisPoint+1].ToString("X2");
                                for (int j = 0; j < frame[analysisPoint + 1] + 1; j++)
                                {
                                    temp += frame[analysisPoint + 1 + j + 1].ToString("X2");
                                }
                                analysisPoint += frame[analysisPoint] + 1;
                            }
                            break;
                        case 4:
                            num = frame[analysisPoint++];
                            temp += num.ToString("X2");
                            for (int i = 0; i < num; i++)
                            {
                                data16 = BitConverter.ToInt16(new byte[] { frame[analysisPoint + 1], frame[analysisPoint] }, 0);
                                temp += data16.ToString("X4");
                                analysisPoint += 2;
                            }
                            break;
                        case 5:
                            num = frame[analysisPoint++];
                            temp += num.ToString("X2");
                            for (int i = 0; i < num; i++)
                            {
                                temp += frame[analysisPoint++].ToString("X2");
                                temp += frame[analysisPoint++].ToString("X2");
                                temp += frame[analysisPoint++].ToString("X2");
                            }
                            break;
                        case 6:
                            num = frame[analysisPoint++];
                            temp += num.ToString("X2");
                            for (int i = 0; i < num; i++)
                            {
                                temp += frame[analysisPoint++].ToString("X2");
                                temp += frame[analysisPoint].ToString("X2");
                                temp += frame[analysisPoint + 1].ToString("X2");
                                for (int j = 0; j < frame[analysisPoint + 1] + 1; j++)
                                {
                                    temp += frame[analysisPoint + 1 + j + 1].ToString("X2");
                                }
                                analysisPoint += frame[analysisPoint] + 1;
                                temp += frame[analysisPoint].ToString("X2");
                                temp += frame[analysisPoint + 1].ToString("X2");
                                for (int j = 0; j < frame[analysisPoint + 1] + 1; j++)
                                {
                                    temp += frame[analysisPoint + 1 + j + 1].ToString("X2");
                                }
                                analysisPoint += frame[analysisPoint] + 1;
                            }
                            break;
                        case 7:
                            num = frame[analysisPoint++];
                            temp += num.ToString("X2");
                            for (int i = 0; i < num; i++)
                            {
                                temp += frame[analysisPoint++].ToString("X2");
                                data16 = BitConverter.ToInt16(new byte[] { frame[analysisPoint + 1], frame[analysisPoint] }, 0);
                                temp += data16.ToString("X4");
                                analysisPoint += 2;
                                data16 = BitConverter.ToInt16(new byte[] { frame[analysisPoint + 1], frame[analysisPoint] }, 0);
                                temp += data16.ToString("X4");
                                analysisPoint += 2;
                            }
                            break;
                        default:
                            break;
                    }
                    list.Add(temp);
                    break;
                case 0x5f:
                    temp = "";
                    for (int i = 0; i < 5; i++)
                    {
                        if (i == 2)
                        {
                            temp += (frame[analysisPoint + i] - 5).ToString("");
                        }
                        else if (i == 3)
                        {
                            temp += (frame[analysisPoint + i] - 1).ToString("");
                        }
                        else
                        {
                            temp += frame[analysisPoint + i].ToString("");
                        }

                        if (i + 1 < 5)
                        {
                            temp += ",";
                        }
                        /*sb.Append(frame[analysisPoint + i].ToString(""));
                        if (i + 1 < 5)
                        {
                            sb.Append(",");
                        }*/

                    }
                    //list.Add(temp);
                    list.Add(temp);
                    
                    analysisPoint += 5;
                    break;
                default:
                    break;
            }
            return list;
        }
        private static string Analysis_Response(byte[] frame, byte frameType)
        {
            StringBuilder sb = new StringBuilder();
            //OAD
            sb.Append("对象属性描述符：");
            string OI = "";
            string OAD = "";
            int attributeID = 0;
            int index = 0;
            int rValue = -1;
            string Oiname = "";
            string temp="";
            List<MeterInfo> meterInfos = new List<MeterInfo>();
            List<PlanInfo> planInfos = new List<PlanInfo>();
            List<object> itemList = new List<object>();
            for (int i = 0; i < 4; i++)
            {
                if (i < 2)
                {
                    OI += frame[analysisPoint].ToString("X2");
                }
                else if (i == 2)
                {
                    attributeID = frame[analysisPoint];
                }
                else
                {
                    index = frame[analysisPoint];
                }
                OAD += frame[analysisPoint].ToString("X2");
                sb.Append(frame[analysisPoint].ToString("X2"));
                analysisPoint++;
            }

            //SqlSugarHelper.Db.Queryable<ObjectID>().Where(o => o._ObjectID == OI);
            if (MainViewModel.TopIndex==0)
            {
                var OiInfo = oIService.SelectNameByOI(OI);
                if (OiInfo != null)
                {
                    var l = SqlSugarHelper.Db.Queryable<ClassAttribute>().Where(c => c.MAttribute == attributeID).Where(c => c.MClass == OiInfo.MIC).Select(c => c.MName).ToList();
                    if (l.Count > 0)
                    {
                        sb.Append($"——{OiInfo.MName}—{l[0]}\r\n\r\n其结果:");
                    }
                    else
                    {
                        sb.Append("\r\n\r\n其结果:");
                    }
                    Oiname = OiInfo.MName;
                }
                else
                {
                    sb.Append("\r\n\r\n其结果:");
                }
            }
            else
            {
                //方案设置界面
                
                sb.Append($"——{BatchParaConfigService.Select(OAD, (frameType - 0x80).ToString())?.ObjectName}\r\n\r\n其结果:");
            }

            switch (frameType)
            {

                case 0x85:
                    //Get-Result∷=CHOICE
                    if (frame[analysisPoint++] == 0)
                    {
                        sb.Append("错误信息:");
                        rValue = frame[analysisPoint++];
                        string ReadR = dataAccessResultService.Select(rValue);
                        sb.Append($"{ReadR}        {rValue}\r\n");
                        if (MainViewModel.TopIndex == 4)
                        {
                            var planSetViewModel = ServiceLocator.Current.GetInstance<PlanSetViewModel>();
                            var item = planSetViewModel.OADReadSeleceds?.Where(b => b.StrOAD.Equals(OAD)).FirstOrDefault();
                            if (item != null)
                            {
                                
                                item.ReadR = ReadR;
                            }

                        }
                    }
                    else
                    {
                        var list = AnalysisData(frame, Oiname, OAD);
                        if (MainViewModel.TopIndex == 4)
                        {
                            var planSetViewModel = ServiceLocator.Current.GetInstance<PlanSetViewModel>();
                            var item = planSetViewModel.OADReadSeleceds?.Where(b => b.StrOAD.Equals(OAD)).FirstOrDefault();
                            if (item!=null)
                            {
                                var GWCmdMakeUpPlanDatas = GWCmdMakeUpPlanService.Select(OAD, item.ClientAPDU);
                                string ReadR = "比对一致";
                                for (int i = 0; i < list.Count; i++)
                                {
                                    if (list[i] is List<object>)
                                    {
                                        itemList = (List<object>)list[i];
                                        
                                    }
                                    else
                                    {
                                        temp = (string)list[i];
                                        if (!temp.Equals(GWCmdMakeUpPlanDatas[i].DataValue))
                                        {
                                            ReadR = "比对不一致";
                                            break;
                                        }
                                    }
                                    
                                }
                                item.ReadR = ReadR;
                            }
                           
                        }
                        if (list.Count>1)
                        {
                            sb.Append($"{list.Count}个元素");
                        }
                        
                        sb.Append("\r\n{");

                        for (int i = 0; i < list.Count; i++)
                        {
                            if (list[i] is List<object>)
                            {
                                itemList = (List<object>)list[i];
                            }
                            else
                            {
                                temp = (string)list[i];
                            }
                            if (Oiname.Contains("功电能") || Oiname.Contains("需量"))
                            {
                                
                                if ((itemList).Count<=0)
                                {
                                    sb.Append("NULL\r\n");
                                    break;
                                }
                                else
                                {
                                    temp= (string)(itemList)[0];
                                }
                                switch (i)
                                {
                                    case 0:
                                        sb.Append("总：");

                                        break;
                                    case 1:
                                        sb.Append("费率1：");

                                        break;
                                    case 2:
                                        sb.Append("费率2：");
                                        break;
                                    case 3:
                                        sb.Append("费率3：");
                                        break;
                                    case 4:
                                        sb.Append("费率4：");
                                        break;
                                    default:
                                        break;
                                }
                                sb.Append(temp + "\r\n");
                            }
                            else if (OAD.Equals("43000300"))
                            {
                                if ((itemList).Count <= 0)
                                {
                                    sb.Append("NULL\r\n");
                                    break;
                                }
                                else
                                {
                                    temp = (string)(itemList)[0];
                                }
                                switch (i)
                                {
                                    case 0:
                                        sb.Append("厂商代码:");
                                        break;
                                    case 1:
                                        sb.Append("软件版本号:");
                                        break;
                                    case 2:
                                        sb.Append("软件版本日期:");
                                        break;
                                    case 3:
                                        sb.Append("硬件版本号:");
                                        break;
                                    case 4:
                                        sb.Append("硬件版本日期:");
                                        break;
                                    case 5:
                                        sb.Append("厂家扩展信息:");
                                        break;
                                    default:
                                        break;
                                }
                                sb.Append(temp + "\r\n");
                            }
                            else if (OAD.Equals("45000200"))
                            {
                                if ((itemList).Count <= 0)
                                {
                                    sb.Append("NULL\r\n");
                                    break;
                                }

                                switch (i)
                                {
                                    case 0:
                                        if (!(itemList[0] is List<object>))
                                        {
                                            temp = (string)itemList[0];
                                        }
                                        sb.Append("工作模式:");
                                        switch (int.Parse(temp))
                                        {
                                            case 0:
                                                sb.Append("混合模式\r\n");
                                                break;
                                            case 1:
                                                sb.Append("客户机模式\r\n");
                                                break;
                                            case 2:
                                                sb.Append("服务器模式\r\n");
                                                break;
                                            default:
                                                break;
                                        }
                                        break;
                                    case 1:
                                        if (!(itemList[0] is List<object>))
                                        {
                                            temp = (string)itemList[0];
                                        }
                                        sb.Append($"在线方式:{(int.Parse(temp) == 0 ? "永久在线" : "被动激活")}\r\n");
                                        break;
                                    case 2:
                                        if (!(itemList[0] is List<object>))
                                        {
                                            temp = (string)itemList[0];
                                        }
                                        sb.Append($"连接方式:{(int.Parse(temp) == 0 ? "TCP" : "UDP")}\r\n");
                                        break;
                                    case 3:
                                        if (!(itemList[0] is List<object>))
                                        {
                                            temp = (string)itemList[0];
                                        }
                                        sb.Append($"连接应用方式:{(int.Parse(temp) == 0 ? "主备模式" : "多连接模式")}\r\n");
                                        break;
                                    case 4:
                                        if (itemList[0] is List<object>)
                                        {
                                            var child = (List<object>)itemList[0];
                                            if (child.Count > 0)
                                            {
                                                sb.Append($"侦听端口列表:{child.Count}个元素\r\n");
                                                for (int j = 0; j < child.Count; j++)
                                                {
                                                    if (!(child[j] is List<object>))
                                                    {
                                                        temp = (string)child[j];
                                                        sb.Append(temp + "\r\n");
                                                    }
                                                    else
                                                    {
                                                        sb.Append("NULL\r\n");
                                                    }
                                                }

                                            }
                                            else
                                            {
                                                sb.Append("侦听端口列表:0个元素\r\n");
                                            }
                                        }
                                        else
                                        {
                                            sb.Append("侦听端口列表:0个元素\r\n");
                                        }
                                        break;
                                    case 5:
                                        if (!(itemList[0] is List<object>))
                                        {
                                            temp = (string)itemList[0];
                                        }
                                        sb.Append($"APN:{temp}\r\n");
                                        break;
                                    case 6:
                                        if (!(itemList[0] is List<object>))
                                        {
                                            temp = (string)itemList[0];
                                        }
                                        sb.Append($"用户名:{temp}\r\n");
                                        break;
                                    case 7:
                                        if (!(itemList[0] is List<object>))
                                        {
                                            temp = (string)itemList[0];
                                        }
                                        sb.Append($"密码:{temp}\r\n");
                                        break;
                                    case 8:
                                        if (!(itemList[0] is List<object>))
                                        {
                                            temp = (string)itemList[0];
                                        }
                                        sb.Append($"代理服务器地址:{temp}\r\n");
                                        break;
                                    case 9:
                                        if (!(itemList[0] is List<object>))
                                        {
                                            temp = (string)itemList[0];
                                        }
                                        sb.Append($"代理端口:{temp}\r\n");
                                        break;
                                    case 10:
                                        if (!(itemList[0] is List<object>))
                                        {
                                            temp = (string)itemList[0];
                                        }
                                        sb.Append($"超时时间:{(byte.Parse(temp) & 0x7c) >> 2}\r\n");
                                        sb.Append($"重发次数:{(byte.Parse(temp) & 0x03)}\r\n");
                                        break;
                                    case 11:
                                        if (!(itemList[0] is List<object>))
                                        {
                                            temp = (string)itemList[0];
                                        }
                                        sb.Append($"心跳周期:{temp}\r\n");
                                        break;
                                    default:
                                        break;
                                }

                            }
                            else if (OAD.Equals("45000300") || OAD.Equals("45100300"))
                            {
                                /*sb.Append($"主站通信参数{i + 1}\r\n");
                                if (itemList[0] is List<object>)
                                {
                                    var child = (List<object>)itemList[0];
                                    if (child.Count > 0)
                                    {
                                        sb.Append($"主站通信参数:{itemList.Count}个元素\r\n");
                                        for (int j = 0; j < itemList.Count; j++)
                                        {
                                            switch (j)
                                            {
                                                case 0:
                                                    if (!(child[j] is List<object>))
                                                    {
                                                        temp = (string)child[j];
                                                       
                                                    }
                                                    sb.Append("{IP地址:");

                                                    sb.Append(string.Format("{0}.{1}.{2}.{3}", Convert.ToInt16(temp.Substring(0, 2), 16), Convert.ToInt16(temp.Substring(2, 2), 16), Convert.ToInt16(temp.Substring(4, 2), 16),
                                                        Convert.ToInt16(temp.Substring(6, 2), 16)));
                                                    sb.Append("\r\n");
                                                    break;
                                                case 1:
                                                    if (!(child[j] is List<object>))
                                                    {
                                                        temp = (string)child[j];
                                                        
                                                    }
                                                    sb.Append("端口:");
                                                    sb.Append(temp + "\r\n");
                                                    sb.Append("}\r\n");
                                                    break;
                                                default:
                                                    break;
                                            }

                                        }

                                    }

                                }*/
                                if (itemList.Count <= 0)
                                {
                                    sb.Append(temp + "\r\n");
                                    break;
                                }
                                else
                                {
                                    sb.Append($"主站通信参数{i + 1}\r\n");
                                    
                                    sb.Append($"主站通信参数:{itemList.Count}个元素\r\n");
                                    sb.Append("{");

                                    for (int j = 0; j < itemList.Count; j++)
                                    {
                                        if (itemList[j] is List<object>)
                                        {
                                            var child = (List<object>)itemList[j];
                                            if (child.Count > 0)
                                            {
                                                if (child.Count > 1)
                                                {
                                                    sb.Append($"{child.Count}个元素\r\n");
                                                    sb.Append("{");
                                                }

                                                foreach (var childitemList in child)
                                                {
                                                    if (!(childitemList is List<object>))
                                                    {
                                                        temp = (string)childitemList;
                                                        switch (j)
                                                        {
                                                            case 0:
                                                                
                                                                sb.Append("{IP地址:");

                                                                sb.Append(string.Format("{0}.{1}.{2}.{3}", Convert.ToInt16(temp.Substring(0, 2), 16), Convert.ToInt16(temp.Substring(2, 2), 16), Convert.ToInt16(temp.Substring(4, 2), 16),
                                                                    Convert.ToInt16(temp.Substring(6, 2), 16)));
                                                                sb.Append("\r\n");
                                                                break;
                                                            case 1:
                                                                sb.Append("端口:");
                                                                sb.Append(temp + "\r\n");
                                                                sb.Append("}\r\n");
                                                                break;
                                                            default:
                                                                break;
                                                        }
                                                    }
                                                    else
                                                    {
                                                        var sonChild = (List<object>)childitemList;

                                                        foreach (var sonchilditemList in sonChild)
                                                        {
                                                            if (!(sonchilditemList is List<object>))
                                                            {
                                                                temp = (string)sonchilditemList;
                                                                sb.Append(temp + "\r\n");
                                                            }

                                                        }
                                                    }
                                                }
                                                if (child.Count > 1)
                                                {
                                                    sb.Append("}\r\n");
                                                }

                                            }
                                            else
                                            {
                                                sb.Append("0个元素\r\n");
                                            }
                                        }
                                        else
                                        {
                                            sb.Append("NULL" + "\r\n");
                                        }
                                    }

                                }

                            }
                            else if (OAD.Equals("45100200"))
                            {
                                switch (i)
                                {
                                    case 0:
                                        if(!(itemList[0] is List<object>))
                                        {
                                            temp = (string)itemList[0];
                                        }
                                        sb.Append("工作模式:");
                                        switch (int.Parse(temp))
                                        {
                                            case 0:
                                                sb.Append("混合模式\r\n");
                                                break;
                                            case 1:
                                                sb.Append("客户机模式\r\n");
                                                break;
                                            case 2:
                                                sb.Append("服务器模式\r\n");
                                                break;
                                            default:
                                                break;
                                        }
                                        break;
                                    case 1:
                                        if (!(itemList[0] is List<object>))
                                        {
                                            temp = (string)itemList[0];
                                        }
                                        sb.Append($"连接方式:{(int.Parse(temp) == 0 ? "TCP" : "UDP")}\r\n");
                                        break;
                                    case 2:
                                        if (!(itemList[0] is List<object>))
                                        {
                                            temp = (string)itemList[0];
                                        }
                                        sb.Append($"连接应用方式:{(int.Parse(temp) == 0 ? "主备模式" : "多连接模式")}\r\n");
                                        break;
                                    case 3:
                                        if (itemList[0] is List<object>)
                                        {
                                            var child = (List<object>)itemList[0];
                                            if (child.Count > 0)
                                            {
                                                sb.Append($"侦听端口列表:{child.Count}个元素\r\n");
                                                for (int j = 0; j < child.Count; j++)
                                                {
                                                    if (!(child[j] is List<object>))
                                                    {
                                                        temp = (string)child[j];
                                                        sb.Append(temp + "\r\n");
                                                    }
                                                    else
                                                    {
                                                        sb.Append("NULL\r\n");
                                                    }
                                                }

                                            }
                                            else
                                            {
                                                sb.Append("侦听端口列表:0个元素\r\n");
                                            }
                                        }
                                        else
                                        {
                                            sb.Append("侦听端口列表:0个元素\r\n");
                                        }
                                        /*if (list[i].Count > 0)
                                        {
                                            sb.Append($"侦听端口列表:{list[i].Count}个元素\r\n");
                                            for (int j = 0; j < list[i].Count; j++)
                                            {
                                                sb.Append(list[i][j] + "\r\n");
                                            }

                                        }
                                        else
                                        {
                                            sb.Append("侦听端口列表:0个元素\r\n");
                                        }*/
                                        break;
                                    case 4:
                                        if (!(itemList[0] is List<object>))
                                        {
                                            temp = (string)itemList[0];
                                        }
                                        sb.Append($"代理服务器地址:{temp}\r\n");
                                        break;
                                    case 5:
                                        if (!(itemList[0] is List<object>))
                                        {
                                            temp = (string)itemList[0];
                                        }
                                        sb.Append($"代理端口:{temp}\r\n");
                                        break;
                                    case 6:
                                        if (!(itemList[0] is List<object>))
                                        {
                                            temp = (string)itemList[0];
                                        }
                                        sb.Append($"超时时间:{(byte.Parse(temp) & 0x7c) >> 2}\r\n");
                                        sb.Append($"重发次数:{(byte.Parse(temp) & 0x03)}\r\n");
                                        break;
                                    case 7:
                                        if (!(itemList[0] is List<object>))
                                        {
                                            temp = (string)itemList[0];
                                        }
                                        sb.Append($"心跳周期:{temp}\r\n");
                                        break;
                                    default:
                                        break;
                                }
                            }
                            else if (OAD.Equals("45100400"))
                            {
                                if (!(itemList[0] is List<object>))
                                {
                                    temp = (string)itemList[0];
                                }
                                switch (i)
                                {
                                    case 0:
                                        sb.Append("IP配置方式:");
                                        switch (int.Parse(temp))
                                        {
                                            case 0:
                                                sb.Append("DHCP\r\n");
                                                break;
                                            case 1:
                                                sb.Append("静态\r\n");
                                                break;
                                            case 2:
                                                sb.Append("PPPoE\r\n");
                                                break;
                                            default:
                                                break;
                                        }
                                        break;
                                    case 1:
                                        sb.Append(string.Format("IP地址:{0}.{1}.{2}.{3}\r\n", Convert.ToInt16(temp.Substring(0, 2), 16), Convert.ToInt16(temp.Substring(2, 2), 16), Convert.ToInt16(temp.Substring(4, 2), 16),
                                                Convert.ToInt16(temp.Substring(6, 2), 16)));
                                        break;
                                    case 2:
                                        sb.Append(string.Format("子网掩码:{0}.{1}.{2}.{3}\r\n", Convert.ToInt16(temp.Substring(0, 2), 16), Convert.ToInt16(temp.Substring(2, 2), 16), Convert.ToInt16(temp.Substring(4, 2), 16),
                                                Convert.ToInt16(temp.Substring(6, 2), 16)));
                                        break;
                                    case 3:
                                        sb.Append(string.Format("网关地址:{0}.{1}.{2}.{3}\r\n", Convert.ToInt16(temp.Substring(0, 2), 16), Convert.ToInt16(temp.Substring(2, 2), 16), Convert.ToInt16(temp.Substring(4, 2), 16),
                                                Convert.ToInt16(temp.Substring(6, 2), 16)));
                                        break;
                                    case 4:
                                        sb.Append($"PPPoE用户名:{temp}\r\n");

                                        break;
                                    case 5:
                                        sb.Append($"PPPoE密码:{temp}\r\n");

                                        break;
                                    default:
                                        break;
                                }
                            }
                            else if(OAD.Equals("60000200"))
                            {
                                MeterInfo meterInfo = new MeterInfo();
                                if (itemList.Count <= 0)
                                {
                                    sb.Append("NULL\r\n");
                                    break;
                                }
                                else if (itemList.Count > 1)
                                {
                                    
                                    sb.Append($"采集档案配置单元array-{i + 1}\r\n");
                                    sb.Append($"采集档案配置单元:{itemList.Count}个元素\r\n");
                                    sb.Append("{");

                                    for (int j = 0; j < itemList.Count; j++)
                                    {
                                        if (itemList[j] is List<object>)
                                        {
                                            var child = (List<object>)itemList[j];
                                            switch (j)
                                            {
                                                case 0:
                                                    sb.Append("配置序号:");
                                                    break;
                                                case 1:
                                                    sb.Append("基本信息:");
                                                    break;
                                                case 2:
                                                    sb.Append("扩展信息:");
                                                    break;
                                                case 3:
                                                    sb.Append("附属信息:");
                                                    break;
                                                default:
                                                    break;
                                            }
                                            if (child.Count > 0)
                                            {
                                                if (child.Count > 1)
                                                {
                                                    sb.Append($"{child.Count}个元素\r\n");
                                                    sb.Append("{");
                                                }
                                                for (int child_i = 0; child_i < child.Count; child_i++)
                                                {
                                                    if (!(child[child_i] is List<object>))
                                                    {
                                                        temp = (string)child[child_i];
                                                        
                                                        sb.Append(temp + "\r\n");
                                                        if (j==0)
                                                        {
                                                            meterInfo.SeqNo = Convert.ToInt16(temp, 10);
                                                            
                                                        }
                                                    }
                                                    else
                                                    {
                                                        var sonChild = (List<object>)child[child_i];
                                                        for (int sonChild_i = 0; sonChild_i < sonChild.Count; sonChild_i++)
                                                        {
                                                            if (!(sonChild[sonChild_i] is List<object>))
                                                            {
                                                                temp = (string)sonChild[sonChild_i];
                                                                switch (j)
                                                                {
                                                                    case 1:
                                                                        switch (child_i)
                                                                        {
                                                                            case 0:
                                                                                sb.Append("通信地址:");
                                                                                sb.Append(temp + "\r\n");
                                                                                meterInfo.CommuAddr = temp;
                                                                                break;
                                                                            case 1:
                                                                                sb.Append("波特率:");
                                                                                sb.Append(Enums.GetBaud(int.Parse(temp)) + "\r\n");
                                                                                meterInfo.Baudrate = int.Parse(temp);
                                                                                break;
                                                                            case 2:
                                                                                sb.Append("规约类型:");
                                                                                sb.Append(Enums.GetAgreementType(int.Parse(temp)) + "\r\n");
                                                                                meterInfo.ProtocolType = int.Parse(temp);
                                                                                break;
                                                                            case 3:
                                                                                sb.Append("端口:");
                                                                                sb.Append(temp + "\r\n");
                                                                                meterInfo.Port = temp;
                                                                                break;
                                                                            case 4:
                                                                                sb.Append("通信密码:");
                                                                                sb.Append(temp + "\r\n");
                                                                                meterInfo.CommuCode = temp;
                                                                                break;
                                                                            case 5:
                                                                                sb.Append("费率个数:");
                                                                                sb.Append(temp + "\r\n");
                                                                                meterInfo.TarriffNum = temp;
                                                                                break;
                                                                            case 6:
                                                                                sb.Append("用户类型:");
                                                                                sb.Append(temp + "\r\n");
                                                                                meterInfo.UserType = temp;
                                                                                break;
                                                                            case 7:
                                                                                sb.Append("接线方式:");
                                                                                sb.Append(Enums.GetConnectType(int.Parse(temp)) + "\r\n");
                                                                                meterInfo.WireMode = int.Parse(temp);
                                                                                break;
                                                                            case 8:
                                                                                sb.Append("额定电压:");
                                                                                temp = (double.Parse(temp) / 10.0).ToString();
                                                                                sb.Append(temp + "\r\n");
                                                                                meterInfo.RatedVoltage = temp;
                                                                                break;
                                                                            case 9:
                                                                                sb.Append("额定电流:");
                                                                                temp = (double.Parse(temp) / 10.0).ToString();
                                                                                sb.Append(temp + "\r\n");
                                                                                meterInfo.RatedCurrent = temp;
                                                                                break;
                                                                            default:
                                                                                break;
                                                                        }
                                                                        break;
                                                                    case 2:
                                                                        switch (child_i)
                                                                        {
                                                                            case 0:
                                                                                sb.Append("采集器地址:");
                                                                                meterInfo.CollectAddr = temp;
                                                                                break;
                                                                            case 1:
                                                                                sb.Append("资产号:");
                                                                                meterInfo.AssetNumber = temp;
                                                                                break;
                                                                            case 2:
                                                                                sb.Append("PT:");
                                                                                meterInfo.Pt = temp;
                                                                                break;
                                                                            case 3:
                                                                                sb.Append("CT:");
                                                                                meterInfo.Ct = temp;
                                                                                break;
                                                                            default:
                                                                                break;
                                                                        }
                                                                        sb.Append(temp + "\r\n");
                                                                        break;
                                                                    default:
                                                                        break;
                                                                }
                                                                
                                                            }
                                                        }
                                                        
                                                    }
                                                }
                                               
                                                if (child.Count > 1)
                                                {
                                                    sb.Append("}\r\n");
                                                }

                                            }
                                            else
                                            {
                                                sb.Append("0个元素\r\n");
                                            }
                                        }
                                        else
                                        {
                                            sb.Append("NULL" + "\r\n");
                                        }
                                    }
                                    
                                }
                                else
                                {
                                    if (!(itemList[0] is List<object>))
                                    {
                                        temp = (string)itemList[0];
                                        sb.Append(temp + "\r\n");
                                    }

                                }
                                if (MainViewModel.TopIndex == 8)
                                {
                                    Application.Current.Dispatcher.Invoke(() => { ServiceLocator.Current.GetInstance<SpecialRecordViewModel>().MeterInfoLIst.Add(meterInfo); });
                                    
                                }
                                //meterInfos.Add(meterInfo);
                            }
                            else if (OAD.Equals("60120200"))
                            {
                                JobInfo jobInfo = new JobInfo();
                                if (itemList.Count <= 0)
                                {
                                    sb.Append("NULL\r\n");
                                    break;
                                }
                                else if (itemList.Count > 1)
                                {

                                    sb.Append($"任务配置单元{i + 1}\r\n");
                                    sb.Append($"任务配置单元:{itemList.Count}个元素\r\n");
                                    sb.Append("{");

                                    for (int j = 0; j < itemList.Count; j++)
                                    {
                                        if (itemList[j] is List<object>)
                                        {
                                            var child = (List<object>)itemList[j];
                                            switch (j)
                                            {
                                                case 0:
                                                    sb.Append("任务ID:");
                                                    break;
                                                case 1:
                                                    sb.Append("执行频率:");
                                                    break;
                                                case 2:
                                                    sb.Append("方案类型:");
                                                    break;
                                                case 3:
                                                    sb.Append("方案编号:");
                                                    break;
                                                case 4:
                                                    sb.Append("开始时间:");
                                                    break;
                                                case 5:
                                                    sb.Append("结束时间:");
                                                    break;
                                                case 6:
                                                    sb.Append("延时时间:");
                                                    break;
                                                case 7:
                                                    sb.Append("执行优先级:");
                                                    break;
                                                case 8:
                                                    sb.Append("状态:");
                                                    break;
                                                case 9:
                                                    sb.Append("任务开始前脚本id:");
                                                    break;
                                                case 10:
                                                    sb.Append("任务完成后脚本id:");
                                                    break;

                                                default:
                                                    break;
                                            }
                                            if (child.Count > 0)
                                            {
                                                if (child.Count > 1)
                                                {
                                                    sb.Append($"{child.Count}个元素\r\n");
                                                    sb.Append("{");
                                                }
                                                for (int child_i = 0; child_i < child.Count; child_i++)
                                                {
                                                    if (!(child[child_i] is List<object>))
                                                    {
                                                        temp = (string)child[child_i];

                                                        sb.Append(temp + "\r\n");
                                                        switch (j)
                                                        {
                                                            case 0:
                                                                jobInfo.JobId = int.Parse(temp);
                                                                break;
                                                            case 1:
                                                                jobInfo.Fre = temp;
                                                                break;
                                                            case 2:
                                                                jobInfo.TypeSelected = int.Parse(temp);
                                                                break;
                                                            case 3:
                                                                jobInfo.Number = int.Parse(temp);
                                                                break;
                                                            case 4:
                                                                jobInfo.StartDateTime = temp;
                                                                break;
                                                            case 5:
                                                                jobInfo.EndDateTime = temp;
                                                                break;
                                                            case 6:
                                                                jobInfo.Delay = temp;
                                                                break;
                                                            case 7:
                                                                jobInfo.Level = int.Parse(temp);
                                                                break;
                                                            case 8:
                                                                jobInfo.StateSelected = int.Parse(temp);
                                                                break;
                                                            case 9:
                                                                jobInfo.RunStartID = int.Parse(temp);
                                                                break;
                                                            case 10:
                                                                jobInfo.RunEndID = int.Parse(temp);
                                                                break;
                                                            default:
                                                                break;
                                                        }

                                                    }
                                                    else
                                                    {
                                                        var sonChild = (List<object>)child[child_i];
                                                        for (int sonChild_i = 0; sonChild_i < sonChild.Count; sonChild_i++)
                                                        {
                                                            if (!(sonChild[sonChild_i] is List<object>))
                                                            {
                                                                temp = (string)sonChild[sonChild_i];
                                                                jobInfo.RunTypeSelected = int.Parse(temp);
                                                               
                                                            }
                                                            else
                                                            {
                                                                //List
                                                                JobConfigTimeData jobConfigTimeData = new JobConfigTimeData();
                                                                var timeList = (List<object>)sonChild[sonChild_i];
                                                                string timeContent = "";
                                                                for (int time_i = 0; time_i < timeList.Count; time_i++)
                                                                {
                                                                    if (!(timeList[time_i] is List<object>))
                                                                    {
                                                                        temp = (string)timeList[time_i];
                                                                        timeContent += temp;
                                                                        sb.Append(temp + "\r\n");
                                                                    }
                                                                    else
                                                                    {
                                                                        temp = (string)((List<object>)timeList[time_i])[0];
                                                                        switch (time_i)
                                                                        {
                                                                            case 0:
                                                                                jobConfigTimeData.StartHour = int.Parse(temp);
                                                                                break;
                                                                            case 1:
                                                                                jobConfigTimeData.StartMin = int.Parse(temp);
                                                                                break;
                                                                            case 2:
                                                                                jobConfigTimeData.EndHour = int.Parse(temp);
                                                                                break;
                                                                            case 3:
                                                                                jobConfigTimeData.EndMin = int.Parse(temp);
                                                                                break;
                                                                            default:
                                                                                break;
                                                                        }
                                                                        sb.Append(temp + "\r\n");
                                                                    }
                                                                }
                                                                jobInfo.TimeList.Add(jobConfigTimeData);
                                                            }
                                                        }

                                                    }
                                                }

                                                if (child.Count > 1)
                                                {
                                                    sb.Append("}\r\n");
                                                }

                                            }
                                            else
                                            {
                                                sb.Append("0个元素\r\n");
                                            }
                                        }
                                        else
                                        {
                                            sb.Append("NULL" + "\r\n");
                                        }
                                    }

                                }
                                else
                                {
                                    if (!(itemList[0] is List<object>))
                                    {
                                        temp = (string)itemList[0];
                                        sb.Append(temp + "\r\n");
                                    }

                                }
                                if (MainViewModel.TopIndex == 8)
                                {
                                    jobInfo.TimeData = string.Join(";",jobInfo.TimeList.Select(x=>x.ToString()));
                                    Application.Current.Dispatcher.Invoke(() => { ServiceLocator.Current.GetInstance<SpecialRecordViewModel>().JobInfoList.Add(jobInfo); });

                                }
                                //planInfos.Add(planInfo);
                            }
                            else if (OAD.Equals("60140200"))
                            {
                                PlanInfo planInfo = new PlanInfo();
                                if (itemList.Count <= 0)
                                {
                                    sb.Append("NULL\r\n");
                                    break;
                                }
                                else if (itemList.Count > 1)
                                {

                                    sb.Append($"普通采集方案array-{i + 1}\r\n");
                                    sb.Append($"普通采集方案:{itemList.Count}个元素\r\n");
                                    sb.Append("{");

                                    for (int j = 0; j < itemList.Count; j++)
                                    {
                                        if (itemList[j] is List<object>)
                                        {
                                            var child = (List<object>)itemList[j];
                                            switch (j)
                                            {
                                                case 0:
                                                    sb.Append("方案序号:");
                                                    break;
                                                case 1:
                                                    sb.Append("存储深度:");
                                                    break;
                                                case 2:
                                                    sb.Append("采集方式:");
                                                    break;
                                                case 3:
                                                    sb.Append("记录列选择:");
                                                    break;
                                                case 4:
                                                    sb.Append("电能表集合:");
                                                    break;
                                                case 5:
                                                    sb.Append("存储时标选择:");
                                                    break;
                                                default:
                                                    break;
                                            }
                                            if (child.Count > 0)
                                            {
                                                if (child.Count > 1)
                                                {
                                                    sb.Append($"{child.Count}个元素\r\n");
                                                    sb.Append("{");
                                                }
                                                for (int child_i = 0; child_i < child.Count; child_i++)
                                                {
                                                    if (!(child[child_i] is List<object>))
                                                    {
                                                        temp = (string)child[child_i];

                                                        sb.Append(temp + "\r\n");
                                                        switch (j)
                                                        {
                                                            case 0:
                                                                planInfo.Number = temp;
                                                                break;
                                                            case 1:
                                                                planInfo.Deep = temp;
                                                                break;
                                                            case 3:
                                                                planInfo.RcsdDataList.Add(new RCSDDataModel { CSD = "对象属性描述符", Data = temp });
                                                                break;
                                                            case 4:
                                                                planInfo.MSData = temp;
                                                                break;
                                                            case 5:
                                                                planInfo.SaveTimeSelected = int.Parse(temp);
                                                                break;
                                                            default:
                                                                break;
                                                        }
                                                        
                                                    }
                                                    else
                                                    {
                                                        var sonChild = (List<object>)child[child_i];
                                                        for (int sonChild_i = 0; sonChild_i < sonChild.Count; sonChild_i++)
                                                        {
                                                            if (!(sonChild[sonChild_i] is List<object>))
                                                            {
                                                                temp = (string)sonChild[sonChild_i];
                                                                switch (j)
                                                                {
                                                                    
                                                                    case 2:
                                                                        switch (child_i)
                                                                        {
                                                                            case 0:
                                                                                sb.Append("采集类型:");
                                                                                planInfo.TypeSelected = int.Parse(temp);
                                                                                break;
                                                                            case 1:
                                                                                sb.Append("采集内容:");
                                                                                planInfo.Content = temp;
                                                                                break;
                                                                            default:
                                                                                break;
                                                                        }
                                                                        sb.Append(temp + "\r\n");
                                                                        break;
                                                                    case 3:
                                                                        planInfo.RcsdDataList.Add(new RCSDDataModel { CSD = "对象属性描述符", Data = temp });
                                                                        sb.Append(temp + "\r\n");
                                                                        break;
                                                                    default:
                                                                        break;
                                                                }

                                                            }
                                                            else
                                                            {
                                                                if (j==2)
                                                                {
                                                                    temp = (string)((List<object>)sonChild[sonChild_i])[0];
                                                                    switch (sonChild_i)
                                                                    {
                                                                        case 0:
                                                                            sb.Append("采集内容:");
                                                                            planInfo.Content = temp;
                                                                            break;
                                                                        case 1:
                                                                            sb.Append("补抄周期:");
                                                                            planInfo.Cycle = temp;
                                                                            break;
                                                                        default:
                                                                            break;
                                                                    }
                                                                    sb.Append(temp + "\r\n");
                                                                }
                                                                else
                                                                {
                                                                    //List
                                                                    var rcsd = (List<object>)sonChild[sonChild_i];
                                                                    string rcsdContent = "";
                                                                    for (int rcsd_i = 0; rcsd_i < rcsd.Count; rcsd_i++)
                                                                    {
                                                                        if (!(rcsd[rcsd_i] is List<object>))
                                                                        {
                                                                            temp = (string)rcsd[rcsd_i];
                                                                            rcsdContent += temp;
                                                                            sb.Append(temp + "\r\n");
                                                                        }
                                                                    }
                                                                    planInfo.RcsdDataList.Add(new RCSDDataModel { CSD = "记录型对象属性描述符", Data = rcsdContent });
                                                                }
                                                                
                                                            }
                                                        }
                                                        
                                                    }
                                                }

                                                if (child.Count > 1)
                                                {
                                                    sb.Append("}\r\n");
                                                }

                                            }
                                            else
                                            {
                                                sb.Append("0个元素\r\n");
                                            }
                                        }
                                        else
                                        {
                                            sb.Append("NULL" + "\r\n");
                                        }
                                    }

                                }
                                else
                                {
                                    if (!(itemList[0] is List<object>))
                                    {
                                        temp = (string)itemList[0];
                                        sb.Append(temp + "\r\n");
                                    }

                                }
                                if (MainViewModel.TopIndex == 8)
                                {
                                    planInfo.RCSDData = string.Join(";",planInfo.RcsdDataList.Select(x=>x.ToString()));
                                    Application.Current.Dispatcher.Invoke(() => { ServiceLocator.Current.GetInstance<SpecialRecordViewModel>().PlanInfoList.Add(planInfo); });

                                }
                                //planInfos.Add(planInfo);
                            }
                            else
                            {
                                if (itemList.Count <= 0)
                                {
                                    sb.Append(temp+"\r\n");
                                    break;
                                }
                                else if (itemList.Count > 1)
                                {
                                    sb.Append($"array-{i + 1}\r\n");
                                    sb.Append($"{itemList.Count}个元素\r\n");
                                    sb.Append("{");

                                    for (int j = 0; j < itemList.Count; j++)
                                    {
                                        if (itemList[j] is List<object>)
                                        {
                                            var child = (List<object>)itemList[j];
                                            if (child.Count > 0)
                                            {
                                                if (child.Count > 1)
                                                {
                                                    sb.Append($"{child.Count}个元素\r\n");
                                                    sb.Append("{");
                                                }
                                                
                                                foreach (var childitemList in child)
                                                {
                                                    if (!(childitemList is List<object>))
                                                    {
                                                        temp = (string)childitemList;
                                                        sb.Append(temp + "\r\n");
                                                    }
                                                    else
                                                    {
                                                        var sonChild = (List<object>)childitemList;
                                                        
                                                        foreach (var sonchilditemList in sonChild)
                                                        {
                                                            if (!(sonchilditemList is List<object>))
                                                            {
                                                                temp = (string)sonchilditemList;
                                                                sb.Append(temp + "\r\n");
                                                            }
                                                           
                                                        }
                                                    }
                                                }
                                                if (child.Count > 1)
                                                {
                                                    sb.Append("}\r\n");
                                                }
                                                
                                            }
                                            else
                                            {
                                                sb.Append("0个元素\r\n");
                                            }
                                        }
                                        else
                                        {
                                            sb.Append("NULL" + "\r\n");
                                        }
                                    }
                                    
                                }
                                else
                                {
                                    if (!(itemList[0] is List<object>))
                                    {
                                        temp = (string)itemList[0];
                                        sb.Append(temp + "\r\n");
                                    }
                                    
                                }

                            }

                        }
                        sb.Append("}\r\n");
                        //string vaivaliData = AnalysisData(frame, Oiname, OAD);
                        //sb.Append(vaivaliData + "\r\n");
                        //BatchParaConfigService.Select(OAD,);
                    }
                    break;
                case 0x83:
                case 0x87:
                case 0x86:
                    //设置执行结果
                    rValue = frame[analysisPoint++];
                    AnalysisResultData = dataAccessResultService.Select(rValue);
                    sb.Append($"{AnalysisResultData}        {rValue}\r\n");
                    
                    break;
                
                case 0x89:
                    if (frame[analysisPoint++] == 0)
                    {
                        sb.Append("错误信息:");
                        rValue = frame[analysisPoint++];
                        sb.Append($"{dataAccessResultService.Select(rValue)}        {rValue}\r\n");

                    }
                    else
                    {
                        sb.Append(Analysis_PROXY(frame) + "\r\n");
                    }
                    break;
                default:
                    break;
            }


            return sb.ToString();
        }
        private static string Analysis_Record(byte[] frame)
        {
            StringBuilder sb = new StringBuilder();
            int rValue = -1;
            //记录型对象属性描述符    	OAD
            sb.Append(GetOADInfo(frame)+"\r\n");
            
            //一行记录N列属性描述符 	RCSD
            int CSDNum = frame[analysisPoint++];
            for (int i = 0; i < CSDNum; i++)
            {
                switch (frame[analysisPoint++])
                {
                    case 0:
                        //OAD
                        sb.Append(GetOADInfo(frame) + "\r\n");
                        break;
                    case 1:
                        //ROAD
                        sb.Append("记录型对象属性描述符：\r\n");
                        sb.Append(GetOADInfo(frame));
                        sb.Append("关联对象属性描述符：\r\n");
                        int OADNum = frame[analysisPoint++];
                        for (int j = 0; j < OADNum; j++)
                        {
                            sb.Append(GetOADInfo(frame));
                        }
                        sb.Append("\r\n");
                        break;
                    default:
                        break;
                }
            }


            //响应数据                  CHOICE
            sb.Append("响应数据\r\n");
            if (frame[analysisPoint++] == 0)
            {
                sb.Append("错误信息：错误信息：-");
                rValue = frame[analysisPoint++];
                sb.Append($"{dataAccessResultService.Select(rValue)}        {rValue}\r\n");

            }
            else
            {
                string temp = "";
                List<object> itemList = new List<object>();
                int recordRowNum = frame[analysisPoint++];
                sb.Append($"{recordRowNum}条记录\r\n");
                for (int i = 0; i < recordRowNum; i++)
                {
                    sb.Append($"第{i+1}条记录\r\n");
                    for (int CSDNum_i = 0; CSDNum_i < CSDNum; CSDNum_i++)
                    {
                        sb.Append($"第{CSDNum_i+1}列数据：");
                        //sb.Append(AnalysisData(frame, "", "") + "\r\n");
                        int dataType = frame[analysisPoint];
                        var list = AnalysisData(frame, "", "");
                        /*if (dataType == 1)
                        {
                            sb.Append($"{list.Count}个元素(array)\r\n");
                        }*/
                        if (list.Count==0)
                        {
                            sb.Append("0个元素\r\n");
                        }
                        for (int j = 0; j < list.Count; j++)
                        {
                            /*if (dataType == 1)
                            {
                                sb.Append($"array-{j + 1}\r\n");
                                for (int k = 0; k < list[j].Count; k++)
                                {

                                }
                            }
                            else
                            {
                                if (list[j].Count > 0)
                                {
                                    sb.Append(list[j][0] + "\r\n");
                                }
                                else
                                {
                                    sb.Append("NULL\r\n");
                                }
                            }*/
                            /*if (list[j].Count <= 0)
                            {
                                sb.Append("NULL\r\n");
                                break;
                            }
                            else if (list[j].Count > 1)
                            {
                                sb.Append($"array-{j + 1}\r\n");
                                sb.Append($"{list[j].Count}个元素\r\n");
                                sb.Append("{");
                                for (int k = 0; k < list[j].Count;k++)
                                {
                                    if (!string.IsNullOrWhiteSpace(list[j][k]))
                                    {
                                        sb.Append(list[j][k] + "\r\n");
                                    }
                                    else
                                    {
                                        sb.Append("NULL" + "\r\n");
                                    }

                                }
                                sb.Append("}\r\n");
                            }
                            else
                            {
                                sb.Append(list[j][0] + "\r\n");
                            }*/
                            if (list[i] is List<object>)
                            {
                                itemList = (List<object>)list[i];
                            }
                            else
                            {
                                temp = (string)list[i];
                            }
                            if (itemList.Count <= 0)
                            {
                                sb.Append(temp + "\r\n");
                                break;
                            }
                            else if (itemList.Count > 1)
                            {
                                sb.Append($"array-{i + 1}\r\n");
                                sb.Append($"{itemList.Count}个元素\r\n");
                                sb.Append("{");

                                for (int itemList_i = 0; itemList_i < itemList.Count; itemList_i++)
                                {
                                    if (itemList[itemList_i] is List<object>)
                                    {
                                        var child = (List<object>)itemList[itemList_i];
                                        if (child.Count > 0)
                                        {
                                            if (child.Count > 1)
                                            {
                                                sb.Append($"{child.Count}个元素\r\n");
                                                sb.Append("{");
                                            }

                                            foreach (var childitemList in child)
                                            {
                                                if (!(childitemList is List<object>))
                                                {
                                                    temp = (string)childitemList;
                                                    sb.Append(temp + "\r\n");
                                                }
                                                else
                                                {
                                                    var sonChild = (List<object>)childitemList;

                                                    foreach (var sonchilditemList in sonChild)
                                                    {
                                                        if (!(sonchilditemList is List<object>))
                                                        {
                                                            temp = (string)sonchilditemList;
                                                            sb.Append(temp + "\r\n");
                                                        }

                                                    }
                                                }
                                            }
                                            if (child.Count > 1)
                                            {
                                                sb.Append("}\r\n");
                                            }

                                        }
                                        else
                                        {
                                            sb.Append("0个元素\r\n");
                                        }
                                    }
                                    else
                                    {
                                        sb.Append("NULL" + "\r\n");
                                    }
                                }

                            }
                            else
                            {
                                if (!(itemList[0] is List<object>))
                                {
                                    temp = (string)itemList[0];
                                    sb.Append(temp + "\r\n");
                                }

                            }
                        }
                        

                    }
                    
                }
               
            }
            return sb.ToString();
        }

        private static string Analysis_Next(byte[] frame)
        {
            StringBuilder sb = new StringBuilder();
            
            List<string> strings = new List<string>();
            sb.Append("分帧响应一个数据块\r\n");
            sb.Append("服务序号-优先级-ACD:" + frame[analysisPoint++].ToString("X2") + "\r\n");
            lastblock = frame[analysisPoint++];
            sb.Append($"末帧标志:{lastblock}\r\n");
            block = BitConverter.ToInt16(new byte[] { frame[analysisPoint+1], frame[analysisPoint ] }, 0);
            sb.Append($"分帧序号:{block}\r\n");
            analysisPoint += 2;
            switch (frame[analysisPoint++])
            {
                case 0:
                    sb.Append("错误信息:");
                    int rValue = frame[analysisPoint++];
                    string ReadR = dataAccessResultService.Select(rValue);
                    sb.Append($"{ReadR}        {rValue}\r\n");
                    break;
                case 1:
                    int num = frame[analysisPoint++];
                    sb.Append($"共有{num}个对象属性及其数据\r\n");
                    for (int i = 0; i < num; i++)
                    {
                        sb.Append($"第{i + 1}个对象属性及其数据\r\n");
                        sb.Append(Analysis_Response(frame, 0x85));
                    }

                    break;
                case 2:
                    num = frame[analysisPoint++];
                    sb.Append($"共有{num}个记录型对象属性及其数据\r\n");
                    for (int i = 0; i < num; i++)
                    {
                        sb.Append($"第{i + 1}个记录型对象属性及其数据\r\n");
                        sb.Append(Analysis_Record(frame));
                    }
                    break;
                default:
                    break;
            }
            return sb.ToString();
        }
        private static string Analysis_Connect(byte[] frame)
        {
            StringBuilder sb = new StringBuilder();
            try
            {
                string temp;
                Int32 data32;
                Int16 data16;
                int result = 0;
                int num = 0;
                sb.Append("厂商代码:");
                for (int i = 0; i < 4; i++)
                {
                    sb.Append((char)frame[analysisPoint++]);
                }
                sb.Append("\r\n软件版本号:");
                for (int i = 0; i < 4; i++)
                {
                    sb.Append((char)frame[analysisPoint++]);
                }
                sb.Append("\r\n软件版本日期:");
                for (int i = 0; i < 6; i++)
                {
                    sb.Append((char)frame[analysisPoint++]);
                }
                sb.Append("\r\n硬件版本号:");
                for (int i = 0; i < 4; i++)
                {
                    sb.Append((char)frame[analysisPoint++]);
                }
                sb.Append("\r\n硬件版本日期:");
                for (int i = 0; i < 6; i++)
                {
                    sb.Append((char)frame[analysisPoint++]);
                }
                sb.Append("\r\n厂家扩展信息:");
                for (int i = 0; i < 8; i++)
                {
                    sb.Append((char)frame[analysisPoint++]);
                }
                sb.Append("\r\n商定的应用层协议版本号:");
                data16 = BitConverter.ToInt16(new byte[] { frame[analysisPoint + 1], frame[analysisPoint] }, 0);
                analysisPoint += 2;
                sb.Append(data16.ToString("X4"));
                sb.Append("\r\n商定的协议一致性块:0x");
                for (int i = 0; i < 8; i++)
                {
                    sb.Append(frame[analysisPoint++].ToString("X2"));
                }
                sb.Append("\r\n商定的功能一致性块:0x");
                for (int i = 0; i < 16; i++)
                {
                    sb.Append(frame[analysisPoint++].ToString("X2"));
                }
                sb.Append("\r\n服务器发送帧最大尺寸:");
                data16 = BitConverter.ToInt16(new byte[] { frame[analysisPoint + 1], frame[analysisPoint] }, 0);
                analysisPoint += 2;
                sb.Append(data16.ToString());
                sb.Append("\r\n服务器接收帧最大尺寸:");
                data16 = BitConverter.ToInt16(new byte[] { frame[analysisPoint + 1], frame[analysisPoint] }, 0);
                analysisPoint += 2;
                sb.Append(data16.ToString());
                sb.Append($"\r\n服务器接收帧最大窗口尺寸:{frame[analysisPoint++]}");
                sb.Append("\r\n服务器最大可处理APDU尺寸:");
                data16 = BitConverter.ToInt16(new byte[] { frame[analysisPoint + 1], frame[analysisPoint] }, 0);
                analysisPoint += 2;
                sb.Append(data16.ToString());
                sb.Append("\r\n商定的应用连接超时时间:");
                data32 = BitConverter.ToInt32(new byte[] { frame[analysisPoint + 3], frame[analysisPoint + 2], frame[analysisPoint + 1], frame[analysisPoint] }, 0);
                analysisPoint += 4;
                sb.Append(data32.ToString());
                result = frame[analysisPoint++];
                sb.Append($"\r\n认证结果:{result}\r\n***允许建立应用连接(0)，密码错误(1)，对称解密错误(2)，非对称解密错误(3)，签名错误(4)，协议版本不匹配(5)，其他错误(255)\r\n");
                if (frame[analysisPoint++] == 0)
                {
                    sb.Append("\r\n无认证附加信息\r\n");
                }
                else
                {
                    num = frame[analysisPoint++];
                    sb.Append("\r\n认证附加信息:{\r\n" + $"服务器随机数_{num}_");
                    temp = "";
                    for (int i = 0; i < num; i++)
                    {
                        temp += frame[analysisPoint++].ToString("X2");
                        //sb.Append(frame[analysisPoint++].ToString("X2"));
                    }
                    sb.Append(temp);
                    SendModule.SessionInit = temp;
                    num = frame[analysisPoint++];
                    sb.Append("\r\n" + $"服务器签名信息_{num}_");
                    temp = "";
                    for (int i = 0; i < num; i++)
                    {
                        temp += frame[analysisPoint++].ToString("X2");
                    }
                    sb.Append(temp);
                    SendModule.Sign = temp;
                    sb.Append("\r\n}\r\n");
                }
                /*if (frame[analysisPoint++] == 0)
                {
                    sb.Append("无跟随上报信息域\r\n");
                }
                else
                {
                    sb.Append("有跟随上报信息域\r\n");
                }
                if (frame[analysisPoint++] == 0)
                {
                    sb.Append("没有时间标签\r\n");
                }
                else
                {
                    sb.Append("有时间标签\r\n");
                }*/
                return sb.ToString();
            }
            catch (Exception)
            {
                return sb.ToString()+"解析失败\r\n";
            }
            
        }

        private static string Analysis_DisConnect(byte[] frame)
        {
            StringBuilder sb = new StringBuilder();
            int rValue = frame[analysisPoint++];
            if (rValue==0)
            {
                SendModule.SessionInitKey = "";
            }
            sb.Append($"结果:{dataAccessResultService.Select(rValue)}        {rValue}\r\n");
            return sb.ToString();
        }
        private static string Analysis_PROXY(byte[] frame)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("返回数据:");
            int length;
            byte b = frame[analysisPoint++];
            if (b == 0x81)
            {
                length = frame[analysisPoint++];
            }
            else if (b == 0x82)
            {
                length = frame[analysisPoint+2];
                analysisPoint += 2;
            }
            else
            {
                length = b;
            }
            for (int i = 0; i < length; i++)
            {
                sb.Append(frame[analysisPoint+i].ToString("X2"));
            }
            analysisPoint += length;
            return sb.ToString();
        }
        public static GPRSInfo AnalysisGPRSLogin(byte[] frame,ref Enums.LinkEnum linkEnum)
        {
            //68 1E 00 81 05 06 00 00 26 08 20 00 F2 50 01 25 00 01 2C 07 E8 02 02 05 09 1C 30 00 A5 E3 1A 16
            GPRSInfo gPRSInfo = new GPRSInfo();
            string temp="";
            
            analysisPoint = 4;
            for (int i = (frame[analysisPoint]&0x0f) + 1; i >0; i--)
            {
                temp += frame[analysisPoint+i].ToString("X2");
            }
            
            gPRSInfo.Address = temp;
            analysisPoint += (frame[analysisPoint]&0x0f) + 1+1;
            analysisPoint += 3;
            //LINK-Request
            int type = frame[analysisPoint++];
            if (type == 0x01)
            {
                
                MessageGenerationModule.SetPIID(frame[analysisPoint++]);
                switch (frame[analysisPoint++])
                {
                    //登录
                    case 0:
                        linkEnum = Enums.LinkEnum.Login;
                        break;
                    //心跳
                    case 1:
                        linkEnum = Enums.LinkEnum.Heart;
                        break;
                    default:
                        break;
                }
                gPRSInfo.HeartCycleTime = BitConverter.ToInt16(new byte []{ frame[analysisPoint+1], frame[analysisPoint] },0);
                analysisPoint += 2;
                if (linkEnum== Enums.LinkEnum.Login)
                {
                    gPRSInfo.LoginDateTime = new DateTime(BitConverter.ToInt16(new byte[] { frame[analysisPoint + 1], frame[analysisPoint] }, 0)
                     , frame[analysisPoint + 2], frame[analysisPoint + 3], frame[analysisPoint + 5], frame[analysisPoint + 6], frame[analysisPoint + 7]
                     , BitConverter.ToInt16(new byte[] { frame[analysisPoint + 9], frame[analysisPoint+8] }, 0));
                }
                gPRSInfo.HeartDateTime = new DateTime(BitConverter.ToInt16(new byte[] { frame[analysisPoint + 1], frame[analysisPoint] }, 0)
                     , frame[analysisPoint + 2], frame[analysisPoint + 3], frame[analysisPoint + 5], frame[analysisPoint + 6], frame[analysisPoint + 7]
                     , BitConverter.ToInt16(new byte[] { frame[analysisPoint + 9], frame[analysisPoint + 8] }, 0));
                
            }
            else if (type == 0x88)
            {
                linkEnum = Enums.LinkEnum.Report;
                switch (frame[analysisPoint++])
                {
                    case 1:
                        gPRSInfo.ReportMode = ReportMode.ReportNotificationList;
                        analysisPoint++;
                        int num = frame[analysisPoint++];
                        
                        for (int i = 0; i < num; i++)
                        {
                            
                        }
                        break;
                    case 2:
                        gPRSInfo.ReportMode = ReportMode.ReportNotificationRecordList;
                        analysisPoint++;
                        num = frame[analysisPoint++];

                        for (int i = 0; i < num; i++)
                        {
                            string OAD = "";
                            for (int i_oad = 0; i_oad < 4; i_oad++)
                            {
                                OAD += frame[analysisPoint].ToString("X2");
                                analysisPoint++;
                            }
                            gPRSInfo.OADList.Add(OAD);

                            //一行记录N列属性描述符 	RCSD
                            int CSDNum = frame[analysisPoint++];
                            for (int j = 0; j < CSDNum; j++)
                            {
                                switch (frame[analysisPoint++])
                                {
                                    case 0:
                                        //OAD
                                        GetOADInfo(frame);
                                        break;
                                    case 1:
                                        //ROAD

                                        GetOADInfo(frame);

                                        int OADNum = frame[analysisPoint++];
                                        for (int z = 0; z < OADNum; z++)
                                        {
                                            GetOADInfo(frame);
                                        }

                                        break;
                                    default:
                                        break;
                                }
                            }


                            //响应数据                  CHOICE

                            if (frame[analysisPoint++] == 0)
                            {
                                analysisPoint++;

                            }
                            else
                            {
                                int recordRowNum = frame[analysisPoint++];

                                for (int j = 0; j < recordRowNum; j++)
                                {

                                    for (int CSDNum_i = 0; CSDNum_i < CSDNum; CSDNum_i++)
                                    {

                                        int dataType = frame[analysisPoint];
                                        var list = AnalysisData(frame, "", "");

                                    }

                                }

                            }
                        }

                        break;
                    default:
                        break;
                }
                
                
            }
            else
            {
                return null;
            }
            return gPRSInfo;
        }
    }
}

